■ ■ ■ ■ ■ ■
common/src/test/java/com/google/tsunami/common/net/http/HttpClientTest.java
| skipped 83 lines |
84 | 84 | | @Test |
85 | 85 | | public void sendAsIs_always_returnsExpectedHttpResponse() |
86 | 86 | | throws IOException, InterruptedException { |
87 | | - | String responseBody = "test response"; |
88 | | - | mockWebServer.enqueue( |
89 | | - | new MockResponse() |
90 | | - | .setResponseCode(HttpStatus.OK.code()) |
91 | | - | .setHeader(CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8.toString()) |
92 | | - | .setBody(responseBody)); |
| 87 | + | mockWebServer.setDispatcher(new SendAsIsTestDispatcher()); |
93 | 88 | | mockWebServer.start(); |
| 89 | + | String expectedResponseBody = SendAsIsTestDispatcher.buildBody("GET", ""); |
94 | 90 | | |
95 | 91 | | HttpUrl baseUrl = mockWebServer.url("/"); |
96 | 92 | | String requestUrl = |
97 | | - | new URL(baseUrl.scheme(), baseUrl.host(), baseUrl.port(), "/%2e%2e/%2e%2e/etc/passwd") |
| 93 | + | new URL( |
| 94 | + | baseUrl.scheme(), |
| 95 | + | baseUrl.host(), |
| 96 | + | baseUrl.port(), |
| 97 | + | "/send-as-is/%2e%2e/%2e%2e/etc/passwd") |
98 | 98 | | .toString(); |
99 | 99 | | |
100 | 100 | | HttpResponse response = httpClient.sendAsIs(get(requestUrl).withEmptyHeaders().build()); |
101 | 101 | | |
102 | | - | assertThat(mockWebServer.takeRequest().getPath()).isEqualTo("/%2e%2e/%2e%2e/etc/passwd"); |
| 102 | + | assertThat(mockWebServer.takeRequest().getPath()) |
| 103 | + | .isEqualTo("/send-as-is/%2e%2e/%2e%2e/etc/passwd"); |
103 | 104 | | assertThat(response) |
104 | 105 | | .isEqualTo( |
105 | 106 | | HttpResponse.builder() |
| skipped 2 lines |
108 | 109 | | HttpHeaders.builder() |
109 | 110 | | .addHeader(CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8.toString()) |
110 | 111 | | // MockWebServer always adds this response header. |
111 | | - | .addHeader(CONTENT_LENGTH, String.valueOf(responseBody.length())) |
| 112 | + | .addHeader(CONTENT_LENGTH, String.valueOf(expectedResponseBody.length())) |
112 | 113 | | .build()) |
113 | | - | .setBodyBytes(ByteString.copyFrom(responseBody, UTF_8)) |
| 114 | + | .setBodyBytes(ByteString.copyFrom(expectedResponseBody, UTF_8)) |
114 | 115 | | .build()); |
115 | 116 | | } |
116 | 117 | | |
117 | 118 | | @Test |
118 | | - | public void sendAsIs_withNonGetRequest_throws() throws IOException, InterruptedException { |
| 119 | + | public void sendAsIs_withPostRequest_returnsExpectedHttpResponse() |
| 120 | + | throws IOException, InterruptedException { |
| 121 | + | mockWebServer.setDispatcher(new SendAsIsTestDispatcher()); |
119 | 122 | | mockWebServer.start(); |
| 123 | + | String requestBody = "POST BODY"; |
| 124 | + | String expectedResponseBody = SendAsIsTestDispatcher.buildBody("POST", requestBody); |
120 | 125 | | |
121 | 126 | | HttpUrl baseUrl = mockWebServer.url("/"); |
122 | 127 | | String requestUrl = |
123 | | - | new URL(baseUrl.scheme(), baseUrl.host(), baseUrl.port(), "/%2e%2e/%2e%2e/etc/passwd") |
| 128 | + | new URL(baseUrl.scheme(), baseUrl.host(), baseUrl.port(), "/send-as-is/%2e%2e/%2e%2e/path") |
124 | 129 | | .toString(); |
125 | 130 | | |
126 | | - | assertThrows( |
127 | | - | IllegalArgumentException.class, |
128 | | - | () -> httpClient.sendAsIs(post(requestUrl).withEmptyHeaders().build())); |
| 131 | + | HttpResponse response = |
| 132 | + | httpClient.sendAsIs( |
| 133 | + | post(requestUrl) |
| 134 | + | .setRequestBody(ByteString.copyFrom(requestBody, UTF_8)) |
| 135 | + | .withEmptyHeaders() |
| 136 | + | .build()); |
| 137 | + | |
| 138 | + | assertThat(mockWebServer.takeRequest().getPath()).isEqualTo("/send-as-is/%2e%2e/%2e%2e/path"); |
| 139 | + | assertThat(response) |
| 140 | + | .isEqualTo( |
| 141 | + | HttpResponse.builder() |
| 142 | + | .setStatus(HttpStatus.OK) |
| 143 | + | .setHeaders( |
| 144 | + | HttpHeaders.builder() |
| 145 | + | .addHeader(CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8.toString()) |
| 146 | + | // MockWebServer always adds this response header. |
| 147 | + | .addHeader(CONTENT_LENGTH, String.valueOf(expectedResponseBody.length())) |
| 148 | + | .build()) |
| 149 | + | .setBodyBytes(ByteString.copyFrom(expectedResponseBody, UTF_8)) |
| 150 | + | .build()); |
129 | 151 | | } |
130 | 152 | | |
131 | 153 | | @Test |
| skipped 627 lines |
759 | 781 | | public MockResponse dispatch(RecordedRequest recordedRequest) { |
760 | 782 | | if (nullToEmpty(recordedRequest.getHeader(HOST)).startsWith(expectedHost)) { |
761 | 783 | | return new MockResponse().setResponseCode(HttpStatus.OK.code()); |
| 784 | + | } |
| 785 | + | return new MockResponse().setResponseCode(HttpStatus.NOT_FOUND.code()); |
| 786 | + | } |
| 787 | + | } |
| 788 | + | |
| 789 | + | static final class SendAsIsTestDispatcher extends Dispatcher { |
| 790 | + | static final String SEND_AS_IS_PATH = "/send-as-is/"; |
| 791 | + | |
| 792 | + | static String buildBody(String method, String requestBody) { |
| 793 | + | return String.format("Method: %s\nRequest Body: %s", method, requestBody); |
| 794 | + | } |
| 795 | + | |
| 796 | + | @Override |
| 797 | + | public MockResponse dispatch(RecordedRequest recordedRequest) { |
| 798 | + | if (recordedRequest.getPath().startsWith(SEND_AS_IS_PATH)) { |
| 799 | + | return new MockResponse() |
| 800 | + | .setHeader(CONTENT_TYPE, MediaType.PLAIN_TEXT_UTF_8.toString()) |
| 801 | + | .setBody(buildBody(recordedRequest.getMethod(), recordedRequest.getBody().readUtf8())) |
| 802 | + | .setResponseCode(HttpStatus.OK.code()); |
762 | 803 | | } |
763 | 804 | | return new MockResponse().setResponseCode(HttpStatus.NOT_FOUND.code()); |
764 | 805 | | } |
| skipped 3 lines |