Mercurial > stress-tester
changeset 756:2042253e3dab
HttpSessionPayloadEventDataReader message no headers bugfix
author | Devel 2 |
---|---|
date | Thu, 14 Dec 2017 11:24:10 +0100 |
parents | 2105a00fae90 |
children | 46e165392894 |
files | stress-tester/src/main/java/com/passus/st/reader/nc/HttpSessionPayloadEventDataReader.java stress-tester/src/main/java/com/passus/st/reader/nc/NcDataHelper.java stress-tester/src/test/java/com/passus/st/reader/nc/HttpSessionPayloadEventDataWriterTest.java stress-tester/src/test/java/com/passus/st/utils/Assert.java stress-tester/src/test/java/com/passus/st/utils/HttpMessageAssert.java |
diffstat | 5 files changed, 198 insertions(+), 41 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/HttpSessionPayloadEventDataReader.java Wed Dec 13 14:55:15 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/HttpSessionPayloadEventDataReader.java Thu Dec 14 11:24:10 2017 +0100 @@ -5,8 +5,10 @@ import com.passus.data.ByteString; import com.passus.data.DataSource; import com.passus.data.HeapByteBuff; +import com.passus.net.http.HttpCachedHeadersImpl; import com.passus.net.http.HttpConsts; import com.passus.net.http.HttpHeaders; +import com.passus.net.http.HttpHeadersDecoder; import com.passus.net.http.HttpHeadersImpl; import com.passus.net.http.HttpMessage; import com.passus.net.http.HttpMethod; @@ -94,26 +96,37 @@ public HttpHeaders decodeHeaders(ByteBuff buffer) throws IOException { long headerSize = ncDataHelper.readLongVLC(buffer); - int startIndex = buffer.startIndex(); - HttpHeaders headers = new HttpHeadersImpl(); - for (;;) { - byte headerCode = buffer.read(); - ByteString headerName; - if (headerCode == CUSTOM_HEADER_CODE) { - headerName = ncDataHelper.readByteStringNullTerminated(buffer); - } else { - headerName = NcHttpDataUtils.codeToHeader(headerCode); - } + HttpHeaders headers; + if (HttpHeadersDecoder.isDefaultEnableCachedHeaders()) { + headers = new HttpHeadersImpl(); + } else { + headers = new HttpCachedHeadersImpl(); + } - ByteString headerValue = ncDataHelper.readByteStringNullTerminated(buffer); - headers.add(headerName, headerValue); + if (headerSize > 0) { + int startIndex = buffer.startIndex(); + for (;;) { + byte headerCode = buffer.read(); + ByteString headerName; + if (headerCode == CUSTOM_HEADER_CODE) { + headerName = ncDataHelper.readByteStringNullTerminated(buffer); + } else { + headerName = NcHttpDataUtils.codeToHeader(headerCode); + if (headerName == null) { + throw new IOException("Unknown header code '" + headerCode + "'."); + } + } - int readed = buffer.startIndex() - startIndex; - if (readed == headerSize) { - break; - } else if (readed > headerSize) { - throw new IOException("Readed bytes > header size."); + ByteString headerValue = ncDataHelper.readByteStringNullTerminated(buffer); + headers.add(headerName, headerValue); + + int readed = buffer.startIndex() - startIndex; + if (readed == headerSize) { + break; + } else if (readed > headerSize) { + throw new IOException("Readed bytes > header size."); + } } }
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/NcDataHelper.java Wed Dec 13 14:55:15 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/NcDataHelper.java Thu Dec 14 11:24:10 2017 +0100 @@ -299,12 +299,8 @@ return len + 1; } - public int readStringNullTerminated(byte[] bytes, int off, Mutable<String> value) { - return readStringTerminated(bytes, off, AsciiUtils.NUL, value); - } - public int readStringTerminated(byte[] bytes, int off, byte delim, Mutable<String> value) { - int index = ByteBuffUtils.localize(bytes, off, AsciiUtils.NUL); + int index = ByteBuffUtils.localize(bytes, off, delim); if (index != -1) { int len = index - off; value.setValue(new String(bytes, off, len, CHARSET)); @@ -314,12 +310,21 @@ throw new IllegalArgumentException("Unable to find delimiter."); } + public int readStringNullTerminated(byte[] bytes, int off, Mutable<String> value) { + return readStringTerminated(bytes, off, AsciiUtils.NUL, value); + } + public String readStringNullTerminated(ByteBuff buffer) { int index = ByteBuffUtils.localize(buffer, AsciiUtils.NUL); if (index != -1) { int startIndex = buffer.startIndex(); int length = index - startIndex; - String value = buffer.toString(startIndex, length, CHARSET); + String value; + if (length == 0) { + value = ""; + } else { + value = buffer.toString(startIndex, length, CHARSET); + } buffer.skipBytes(length + 1); return value; }
--- a/stress-tester/src/test/java/com/passus/st/reader/nc/HttpSessionPayloadEventDataWriterTest.java Wed Dec 13 14:55:15 2017 +0100 +++ b/stress-tester/src/test/java/com/passus/st/reader/nc/HttpSessionPayloadEventDataWriterTest.java Thu Dec 14 11:24:10 2017 +0100 @@ -1,17 +1,22 @@ package com.passus.st.reader.nc; +import static com.passus.data.DataSourceUtis.toByteBuff; import com.passus.data.HeapByteBuff; +import com.passus.net.http.HttpConsts; import com.passus.net.http.HttpHeaders; import com.passus.net.http.HttpHeadersImpl; import com.passus.net.http.HttpRequest; import com.passus.net.http.HttpRequestBuilder; +import com.passus.net.http.HttpResponse; +import com.passus.net.http.HttpResponseBuilder; +import com.passus.st.client.http.HttpReqResp; import com.passus.st.client.http.HttpSessionPayloadEvent; import com.passus.st.emitter.SessionInfo; +import static com.passus.st.utils.HttpMessageAssert.*; import java.io.File; import java.io.IOException; import java.util.UUID; -import org.apache.commons.io.FileUtils; -import static org.testng.Assert.*; +import static org.testng.AssertJUnit.*; import org.testng.annotations.Test; /** @@ -41,32 +46,138 @@ headers.add(HttpHeaders.HOST, "test"); headers.add("X-HEADER", "X-HEADER-VALUE"); HeapByteBuff buffer = new HeapByteBuff(); - + HttpSessionPayloadEventDataWriter payloadWriter = new HttpSessionPayloadEventDataWriter(); payloadWriter.encodeHeaders(headers, buffer); assertEquals(33, buffer.readableBytes()); } - + + private void writeMessages(File file, HttpRequest req, HttpResponse resp) throws Exception { + HttpSessionPayloadEventDataWriter payloadWriter = new HttpSessionPayloadEventDataWriter(); + try (NcDataBlockWriter writer = new NcDataBlockWriter(file)) { + writer.open(); + payloadWriter.write(new HttpSessionPayloadEvent(session, req, resp, ""), writer); + } + } + + private NcSessionPayloadBlock readPayloadBlock(File file) throws IOException { + try (NcDataBlockReader reader = new NcDataBlockReader(file)) { + reader.open(); + + NcDataBlock block; + while ((block = reader.read()) != null) { + if (block.type() == NcSessionPayloadBlock.TYPE) { + return (NcSessionPayloadBlock) block; + } + } + } + + throw new IOException("Unble to find payload block."); + } + @Test public void testWrite_HttpRequest() throws Exception { File file = createTmpFile(); try { - try (NcDataBlockWriter writer = new NcDataBlockWriter(file)) { - writer.open(); + HttpRequest req = HttpRequestBuilder.get("http://test.com/test") + .header("X-Header", "X-Header-Value") + .content("content") + .build(); - HttpSessionPayloadEventDataWriter payloadWriter = new HttpSessionPayloadEventDataWriter(); - HttpRequest req = HttpRequestBuilder.get("http://test.com/test") - .header("X-Header", "X-Header-Value") - .content("content") - .build(); + writeMessages(file, req, null); - payloadWriter.write(new HttpSessionPayloadEvent(session, req, null, ""), writer); - } - - byte[] content = FileUtils.readFileToByteArray(file); + NcSessionPayloadBlock payload = readPayloadBlock(file); + HttpSessionPayloadEventDataReader payloadReader = new HttpSessionPayloadEventDataReader(); + HttpReqResp reqResp = payloadReader.decodeMessages(toByteBuff(payload.data())); + assertFalse(reqResp == null); + assertTrue(reqResp.getResponse() == null); + assertMessages(req, reqResp.getRequest()); + assertMessagesContent(req, reqResp.getRequest()); } finally { file.delete(); } } + @Test + public void testWrite_HttpRequest_NoContent() throws Exception { + File file = createTmpFile(); + try { + HttpRequest req = HttpRequestBuilder.get("http://test.com/test") + .header("X-Header", "X-Header-Value") + .build(); + + writeMessages(file, req, null); + + NcSessionPayloadBlock payload = readPayloadBlock(file); + HttpSessionPayloadEventDataReader payloadReader = new HttpSessionPayloadEventDataReader(); + HttpReqResp reqResp = payloadReader.decodeMessages(toByteBuff(payload.data())); + assertFalse(reqResp == null); + assertTrue(reqResp.getResponse() == null); + assertMessages(req, reqResp.getRequest()); + assertMessagesContent(req, reqResp.getRequest()); + } finally { + file.delete(); + } + } + + @Test + public void testWrite_HttpRequest_NoHeaders() throws Exception { + File file = createTmpFile(); + try { + HttpRequest req = HttpRequestBuilder.get("http://test.com/test") + .version(HttpConsts.VERSION_1_0) + .build(); + + writeMessages(file, req, null); + + NcSessionPayloadBlock payload = readPayloadBlock(file); + HttpSessionPayloadEventDataReader payloadReader = new HttpSessionPayloadEventDataReader(); + HttpReqResp reqResp = payloadReader.decodeMessages(toByteBuff(payload.data())); + assertFalse(reqResp == null); + assertTrue(reqResp.getResponse() == null); + assertMessages(req, reqResp.getRequest()); + } finally { + file.delete(); + } + } + + @Test + public void testWrite_HttpResponse() throws Exception { + File file = createTmpFile(); + try { + HttpResponse resp = HttpResponseBuilder.ok() + .header("X-Header", "X-Header-Value") + .content("test") + .build(); + + writeMessages(file, null, resp); + + NcSessionPayloadBlock payload = readPayloadBlock(file); + HttpSessionPayloadEventDataReader payloadReader = new HttpSessionPayloadEventDataReader(); + HttpReqResp reqResp = payloadReader.decodeMessages(toByteBuff(payload.data())); + assertFalse(reqResp == null); + assertTrue(reqResp.getRequest() == null); + assertMessages(resp, reqResp.getResponse()); + assertMessagesContent(resp, reqResp.getResponse()); + } finally { + file.delete(); + } + } + + @Test + public void testWrite_NoMessage() throws Exception { + File file = createTmpFile(); + try { + writeMessages(file, null, null); + + NcSessionPayloadBlock payload = readPayloadBlock(file); + HttpSessionPayloadEventDataReader payloadReader = new HttpSessionPayloadEventDataReader(); + HttpReqResp reqResp = payloadReader.decodeMessages(toByteBuff(payload.data())); + assertFalse(reqResp == null); + assertTrue(reqResp.getRequest() == null); + assertTrue(reqResp.getResponse() == null); + } finally { + file.delete(); + } + } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/test/java/com/passus/st/utils/Assert.java Thu Dec 14 11:24:10 2017 +0100 @@ -0,0 +1,30 @@ +package com.passus.st.utils; + +import com.passus.data.ByteString; +import com.passus.data.ByteStringImpl; +import com.passus.data.SliceByteString; +import org.testng.AssertJUnit; +import static org.testng.AssertJUnit.assertEquals; + +/** + * + * @author Mirosław Hawrot + */ +public class Assert extends AssertJUnit { + + public static void assertEquals(ByteString expected, ByteString actual) { + assertEquals(null, expected.toString(), actual.toString()); + } + + public static void assertEquals(String expected, ByteString actual) { + ByteString expectedBs; + if (actual instanceof SliceByteString) { + expectedBs = new SliceByteString(expected); + } else { + expectedBs = new ByteStringImpl(expected); + } + + assertEquals(null, expectedBs, actual); + } + +}
--- a/stress-tester/src/test/java/com/passus/st/utils/HttpMessageAssert.java Wed Dec 13 14:55:15 2017 +0100 +++ b/stress-tester/src/test/java/com/passus/st/utils/HttpMessageAssert.java Thu Dec 14 11:24:10 2017 +0100 @@ -1,5 +1,6 @@ package com.passus.st.utils; +import static com.passus.st.utils.Assert.*; import com.passus.data.ByteBuff; import com.passus.data.ByteString; import com.passus.data.HeapByteBuff; @@ -7,11 +8,8 @@ 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; -import static org.testng.AssertJUnit.fail; /** *