changeset 691:9ccc2452b92c

NC file in progress
author Devel 2
date Mon, 27 Nov 2017 10:49:54 +0100
parents fdeaeb7d4da2
children a27658c9e2e3
files stress-tester/src/main/java/com/passus/st/client/SessionStatusEvent.java stress-tester/src/main/java/com/passus/st/reader/nc/HttpSessionPayloadEventDataReader.java stress-tester/src/main/java/com/passus/st/reader/nc/HttpSessionPayloadEventDataWriter.java stress-tester/src/test/java/com/passus/st/reader/nc/HttpSessionPayloadEventDataReaderTest.java stress-tester/src/test/java/com/passus/st/utils/HttpMessageAssert.java
diffstat 5 files changed, 173 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/SessionStatusEvent.java	Mon Nov 27 09:57:23 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/SessionStatusEvent.java	Mon Nov 27 10:49:54 2017 +0100
@@ -23,9 +23,9 @@
     }
 
     public SessionStatusEvent(SessionInfo sessionInfo, int status) {
-        this(sessionInfo, status, null);
+        this(sessionInfo, status, sessionInfo.getSourceName());
     }
-    
+
     public SessionStatusEvent(SessionInfo sessionInfo, int status, String sourceName) {
         super(sessionInfo, sourceName);
 
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/HttpSessionPayloadEventDataReader.java	Mon Nov 27 09:57:23 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/HttpSessionPayloadEventDataReader.java	Mon Nov 27 10:49:54 2017 +0100
@@ -23,7 +23,7 @@
 
     private final DataHelper dataHelper = DataHelper.BIG_ENDIAN;
 
-    private ByteString readVersion(ByteBuff buff) throws IOException {
+    private ByteString decodeVersion(ByteBuff buff) throws IOException {
         byte b = buff.read();
         if (b == NcHttpDataUtils.VERSION_1_0) {
             return HttpConsts.VERSION_1_0;
@@ -34,7 +34,7 @@
         throw new IOException("Not supported HTTP version '" + b + "'.");
     }
 
-    public HttpHeaders readHeaders(ByteBuff buffer) throws IOException {
+    private HttpHeaders decodeHeaders(ByteBuff buffer) throws IOException {
         long headerSize = ncDataHelper.readLongVLC(buffer);
         int startIndex = buffer.startIndex();
 
@@ -62,15 +62,15 @@
         return headers;
     }
 
-    private HttpMessage readMessage(ByteBuff buffer) throws IOException {
-        int flags = dataHelper.readInt4(buffer);
+    public HttpMessage decodeMessage(ByteBuff buffer) throws IOException {
+        byte flags = buffer.read();
 
         HttpMessage msg;
         if ((flags & FLAG_REQUEST) != 0) {
             ByteString method = ncDataHelper.readByteStringNullTerminated(buffer);
             ByteString uri = ncDataHelper.readByteStringNullTerminated(buffer);
 
-            msg = new HttpRequest(method, uri);
+            msg = new HttpRequest(uri, method);
         } else {
             int statusCode = dataHelper.readInt2(buffer);
             ByteString reasonPhrase = ncDataHelper.readByteStringNullTerminated(buffer);
@@ -79,11 +79,8 @@
             msg = new HttpResponse(status);
         }
 
-        msg.setVersion(readVersion(buffer));
-
-        HttpHeaders headers = readHeaders(buffer);
-        msg.setHeaders(headers);
-
+        msg.setVersion(decodeVersion(buffer));
+        msg.setHeaders(decodeHeaders(buffer));
         return msg;
     }
 
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/HttpSessionPayloadEventDataWriter.java	Mon Nov 27 09:57:23 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/HttpSessionPayloadEventDataWriter.java	Mon Nov 27 10:49:54 2017 +0100
@@ -26,7 +26,7 @@
 
     private final DataHelper dataHelper = DataHelper.get(ByteOrder.BIG_ENDIAN);
 
-    private int writeHeaders(HttpHeaders headers, ByteBuff buff) {
+    public int encodeHeaders(HttpHeaders headers, ByteBuff buff) {
         ByteBuff headersBuff = new HeapByteBuff();
         for (HttpHeaderEntry entry : headers.getEntries()) {
             int code = NcHttpDataUtils.headerToCode(entry.getName());
@@ -46,7 +46,7 @@
         return len;
     }
 
-    private void writeVersion(HttpMessage msg, ByteBuff buff) throws IOException {
+    public void encodeVersion(HttpMessage msg, ByteBuff buff) throws IOException {
         if (HttpConsts.VERSION_1_0.equals(msg.getVersion())) {
             buff.append(NcHttpDataUtils.VERSION_1_0);
         } else if (HttpConsts.VERSION_1_1.equals(msg.getVersion())) {
@@ -56,58 +56,65 @@
         }
     }
 
-    private void writeMessage(long timestamp, SessionInfo session, HttpMessage msg, NcDataBlockWriter writer) throws IOException {
-        if (msg != null) {
-            long len = 0;
-            DataSource content = msg.getContent();
-            ByteBuff buff = new HeapByteBuff();
-            byte flags = 0;
-            if (msg.isRequest()) {
-                flags = FLAG_REQUEST;
-                HttpRequest req = (HttpRequest) msg;
-
-                buff.append(flags);
-                len++;
-                len += ncDataHelper.writeByteStringNullTerminated(req.getMethod().toByteString(), buff);
-                len += ncDataHelper.writeByteStringNullTerminated(req.getUri(), buff);
-
-                writeVersion(msg, buff);
-                len++;
-            } else {
-                HttpResponse resp = (HttpResponse) msg;
+    public long encodeMessage(HttpMessage msg, ByteBuff buff) throws IOException {
+        long size = 0;
+        byte flags = 0;
+        if (msg.isRequest()) {
+            flags = FLAG_REQUEST;
+            HttpRequest req = (HttpRequest) msg;
 
-                buff.append(flags);
-                len++;
-                dataHelper.writeInt2(buff, resp.getStatus().getCode());
-                len += 2;
-                len += ncDataHelper.writeByteStringNullTerminated(resp.getStatus().getReasonPhrase(), buff);
-
-                writeVersion(msg, buff);
-                len++;
-            }
+            buff.append(flags);
+            size++;
+            size += ncDataHelper.writeByteStringNullTerminated(req.getMethod().toByteString(), buff);
+            size += ncDataHelper.writeByteStringNullTerminated(req.getUri(), buff);
 
-            len += writeHeaders(msg.getHeaders(), buff);
-            if (content != null) {
-                len += content.available();
-            }
+            encodeVersion(msg, buff);
+            size++;
+        } else {
+            HttpResponse resp = (HttpResponse) msg;
 
-            writer.writeSessionPayloadHeader(timestamp, session, (byte) 1, len);
-            writer.writeSessionPayloadContent(buff);
-            
-            if (content != null) {
-                writer.writeSessionPayloadContent(content);
-            }
-            
-            writer.closeSessionPayloadBlock();
+            buff.append(flags);
+            size++;
+            dataHelper.writeInt2(buff, resp.getStatus().getCode());
+            size += 2;
+            size += ncDataHelper.writeByteStringNullTerminated(resp.getStatus().getReasonPhrase(), buff);
+
+            encodeVersion(msg, buff);
+            size++;
         }
+
+        size += encodeHeaders(msg.getHeaders(), buff);
+        return size;
+    }
+
+    private void encodeFullMessage(long timestamp, SessionInfo session, HttpMessage msg, NcDataBlockWriter writer) throws IOException {
+        ByteBuff buffer = new HeapByteBuff();
+        long size = encodeMessage(msg, buffer);
+        DataSource content = msg.getContent();
+        if (content != null) {
+            size += content.available();
+        }
+
+        writer.writeSessionPayloadHeader(timestamp, session, (byte) 1, size);
+        writer.writeSessionPayloadData(buffer);
+        if (content != null) {
+            writer.writeSessionPayloadData(content);
+        }
+
+        writer.closeSessionPayloadBlock();
     }
 
     public void write(HttpSessionPayloadEvent event, NcDataBlockWriter writer) throws IOException {
         long time = event.getTimestamp();
         SessionInfo session = event.getSessionInfo();
 
-        writeMessage(time, session, event.getRequest(), writer);
-        writeMessage(time, session, event.getResponse(), writer);
+        if (event.getRequest() != null) {
+            encodeFullMessage(time, session, event.getRequest(), writer);
+        }
+
+        if (event.getResponse() != null) {
+            encodeFullMessage(time, session, event.getResponse(), writer);
+        }
     }
 
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/reader/nc/HttpSessionPayloadEventDataReaderTest.java	Mon Nov 27 10:49:54 2017 +0100
@@ -0,0 +1,36 @@
+package com.passus.st.reader.nc;
+
+import com.passus.data.ByteBuff;
+import com.passus.data.HeapByteBuff;
+import com.passus.net.http.HttpMessage;
+import com.passus.net.http.HttpRequest;
+import com.passus.net.http.HttpRequestBuilder;
+import java.io.IOException;
+import org.testng.annotations.Test;
+import static com.passus.st.utils.HttpMessageAssert.*;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+public class HttpSessionPayloadEventDataReaderTest {
+
+    private void encodeMessage(HttpMessage msg, ByteBuff buffer) throws IOException {
+        HttpSessionPayloadEventDataWriter writer = new HttpSessionPayloadEventDataWriter();
+        writer.encodeMessage(msg, buffer);
+    }
+
+    @Test
+    public void testDecodeMessage() throws Exception {
+        ByteBuff buffer = new HeapByteBuff();
+        HttpRequest req = HttpRequestBuilder.get("http://test.com/test")
+                .header("X-Header", "X-Header-Value")
+                .build();
+        encodeMessage(req, buffer);
+
+        HttpSessionPayloadEventDataReader reader = new HttpSessionPayloadEventDataReader();
+        HttpMessage msgDecoded = reader.decodeMessage(buffer);
+        assertMessages(req, msgDecoded);
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/utils/HttpMessageAssert.java	Mon Nov 27 10:49:54 2017 +0100
@@ -0,0 +1,77 @@
+package com.passus.st.utils;
+
+import com.passus.data.ByteString;
+import com.passus.net.http.HttpHeaders;
+import com.passus.net.http.HttpMessage;
+import com.passus.net.http.HttpRequest;
+import com.passus.net.http.HttpResponse;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import static org.testng.AssertJUnit.assertEquals;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+public class HttpMessageAssert {
+
+    private HttpMessageAssert() {
+    }
+
+    public static void assertMessages(HttpMessage expectedMsg, HttpMessage msg) {
+        if (expectedMsg == msg) {
+            return;
+        }
+
+        assertEquals("Different messages type.", expectedMsg.isRequest(), msg.isRequest());
+        if (expectedMsg.isRequest()) {
+            assertRequests((HttpRequest) expectedMsg, (HttpRequest) msg);
+        } else {
+            assertResponses((HttpResponse) expectedMsg, (HttpResponse) msg);
+        }
+    }
+
+    public static void assertRequests(HttpRequest expectedReq, HttpRequest req) {
+        if (expectedReq == req) {
+            return;
+        }
+
+        assertEquals(expectedReq.getMethod(), req.getMethod());
+        assertEquals(expectedReq.getUri(), req.getUri());
+        assertEquals(expectedReq.getVersion(), req.getVersion());
+        assertHeaders(expectedReq.getHeaders(), req.getHeaders());
+    }
+
+    public static void assertResponses(HttpResponse expectedResp, HttpResponse resp) {
+        if (expectedResp == resp) {
+            return;
+        }
+
+        assertEquals(expectedResp.getStatus().getCode(), resp.getStatus().getCode());
+        assertEquals(expectedResp.getStatus().getReasonPhrase(), resp.getStatus().getReasonPhrase());
+        assertEquals(expectedResp.getVersion(), resp.getVersion());
+        assertHeaders(expectedResp.getHeaders(), resp.getHeaders());
+    }
+
+    public static void assertHeaders(HttpHeaders expectedHeaders, HttpHeaders hdrs) {
+        if (expectedHeaders == hdrs) {
+            return;
+        }
+
+        Set<ByteString> headersName1 = expectedHeaders.getNames();
+        Set<ByteString> headersName2 = hdrs.getNames();
+        assertEquals(headersName1.size(), headersName2.size());
+        for (ByteString name : headersName1) {
+            List<ByteString> values1 = expectedHeaders.getAll(name);
+            List<ByteString> values2 = hdrs.getAll(name);
+            assertEquals(values1, values2);
+        }
+
+        for (ByteString name : headersName2) {
+            List<ByteString> values1 = expectedHeaders.getAll(name);
+            List<ByteString> values2 = hdrs.getAll(name);
+            assertEquals(values2, values1);
+        }
+    }
+}