|
| 1 | +/* |
| 2 | + * Copyright © 2012 The Feign Authors ([email protected]) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package feign; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 21 | + |
| 22 | +import feign.Request.HttpMethod; |
| 23 | +import java.util.Collection; |
| 24 | +import java.util.Collections; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +public class LoggerRebufferTest { |
| 28 | + |
| 29 | + private static final String CONFIG_KEY = "TestApi#testMethod()"; |
| 30 | + |
| 31 | + private static class TestLogger extends Logger { |
| 32 | + @Override |
| 33 | + protected void log(String configKey, String format, Object... args) {} |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void headersLevelRebuffersResponseBody() throws Exception { |
| 38 | + // given |
| 39 | + TestLogger logger = new TestLogger(); |
| 40 | + String originalBody = "{\"status\":\"error\",\"message\":\"Not found\"}"; |
| 41 | + Response response = |
| 42 | + Response.builder() |
| 43 | + .status(404) |
| 44 | + .reason("Not Found") |
| 45 | + .request( |
| 46 | + Request.create( |
| 47 | + HttpMethod.GET, "/api/resource", Collections.emptyMap(), null, Util.UTF_8)) |
| 48 | + .headers(Collections.<String, Collection<String>>emptyMap()) |
| 49 | + .body(originalBody, Util.UTF_8) |
| 50 | + .build(); |
| 51 | + |
| 52 | + // when |
| 53 | + Response result = |
| 54 | + logger.logAndRebufferResponse(CONFIG_KEY, Logger.Level.HEADERS, response, 100); |
| 55 | + |
| 56 | + // then |
| 57 | + String read1 = Util.toString(result.body().asReader(Util.UTF_8)); |
| 58 | + String read2 = Util.toString(result.body().asReader(Util.UTF_8)); |
| 59 | + assertEquals(originalBody, read1, "First read should return original body"); |
| 60 | + assertEquals(originalBody, read2, "Second read should return same body (rebuffered)"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void basicLevelDoesNotRebufferResponseBody() throws Exception { |
| 65 | + // given |
| 66 | + TestLogger logger = new TestLogger(); |
| 67 | + String originalBody = "{\"status\":\"ok\"}"; |
| 68 | + Response response = |
| 69 | + Response.builder() |
| 70 | + .status(200) |
| 71 | + .reason("OK") |
| 72 | + .request( |
| 73 | + Request.create( |
| 74 | + HttpMethod.GET, "/api/resource", Collections.emptyMap(), null, Util.UTF_8)) |
| 75 | + .headers(Collections.<String, Collection<String>>emptyMap()) |
| 76 | + .body(originalBody, Util.UTF_8) |
| 77 | + .build(); |
| 78 | + |
| 79 | + // when |
| 80 | + Response result = logger.logAndRebufferResponse(CONFIG_KEY, Logger.Level.BASIC, response, 100); |
| 81 | + |
| 82 | + // then |
| 83 | + String read1 = Util.toString(result.body().asReader(Util.UTF_8)); |
| 84 | + assertEquals(originalBody, read1, "First read should return original body"); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void fullLevelRebuffersResponseBody() throws Exception { |
| 89 | + // given |
| 90 | + TestLogger logger = new TestLogger(); |
| 91 | + String originalBody = "{\"data\":{\"id\":123,\"name\":\"test\"}}"; |
| 92 | + Response response = |
| 93 | + Response.builder() |
| 94 | + .status(200) |
| 95 | + .reason("OK") |
| 96 | + .request( |
| 97 | + Request.create( |
| 98 | + HttpMethod.POST, "/api/create", Collections.emptyMap(), null, Util.UTF_8)) |
| 99 | + .headers(Collections.<String, Collection<String>>emptyMap()) |
| 100 | + .body(originalBody, Util.UTF_8) |
| 101 | + .build(); |
| 102 | + |
| 103 | + // when |
| 104 | + Response result = logger.logAndRebufferResponse(CONFIG_KEY, Logger.Level.FULL, response, 50); |
| 105 | + |
| 106 | + // then |
| 107 | + String read1 = Util.toString(result.body().asReader(Util.UTF_8)); |
| 108 | + String read2 = Util.toString(result.body().asReader(Util.UTF_8)); |
| 109 | + String read3 = Util.toString(result.body().asReader(Util.UTF_8)); |
| 110 | + assertEquals(originalBody, read1, "First read should return original body"); |
| 111 | + assertEquals(originalBody, read2, "Second read should return same body"); |
| 112 | + assertEquals(originalBody, read3, "Third read should return same body"); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void noneLevelDoesNotRebufferResponseBody() throws Exception { |
| 117 | + // given |
| 118 | + TestLogger logger = new TestLogger(); |
| 119 | + String originalBody = "{\"result\":\"success\"}"; |
| 120 | + Response response = |
| 121 | + Response.builder() |
| 122 | + .status(200) |
| 123 | + .reason("OK") |
| 124 | + .request( |
| 125 | + Request.create( |
| 126 | + HttpMethod.GET, "/api/status", Collections.emptyMap(), null, Util.UTF_8)) |
| 127 | + .headers(Collections.<String, Collection<String>>emptyMap()) |
| 128 | + .body(originalBody, Util.UTF_8) |
| 129 | + .build(); |
| 130 | + |
| 131 | + // When |
| 132 | + Response result = logger.logAndRebufferResponse(CONFIG_KEY, Logger.Level.NONE, response, 100); |
| 133 | + |
| 134 | + // then |
| 135 | + String read1 = Util.toString(result.body().asReader(Util.UTF_8)); |
| 136 | + assertEquals(originalBody, read1, "Body should be readable"); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void http204DoesNotRebufferEvenAtHeadersLevel() throws Exception { |
| 141 | + // given |
| 142 | + TestLogger logger = new TestLogger(); |
| 143 | + Response response = |
| 144 | + Response.builder() |
| 145 | + .status(204) |
| 146 | + .reason("No Content") |
| 147 | + .request( |
| 148 | + Request.create( |
| 149 | + HttpMethod.DELETE, "/api/resource/1", Collections.emptyMap(), null, Util.UTF_8)) |
| 150 | + .headers(Collections.<String, Collection<String>>emptyMap()) |
| 151 | + .body("should be ignored", Util.UTF_8) |
| 152 | + .build(); |
| 153 | + |
| 154 | + // when |
| 155 | + Response result = |
| 156 | + logger.logAndRebufferResponse(CONFIG_KEY, Logger.Level.HEADERS, response, 100); |
| 157 | + |
| 158 | + // then |
| 159 | + assertNotNull(result.body(), "Response body object should exist"); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + void http205DoesNotRebufferEvenAtHeadersLevel() throws Exception { |
| 164 | + // given |
| 165 | + TestLogger logger = new TestLogger(); |
| 166 | + Response response = |
| 167 | + Response.builder() |
| 168 | + .status(205) |
| 169 | + .reason("Reset Content") |
| 170 | + .request( |
| 171 | + Request.create( |
| 172 | + HttpMethod.POST, "/api/form", Collections.emptyMap(), null, Util.UTF_8)) |
| 173 | + .headers(Collections.<String, Collection<String>>emptyMap()) |
| 174 | + .body("should be ignored", Util.UTF_8) |
| 175 | + .build(); |
| 176 | + |
| 177 | + // when |
| 178 | + Response result = |
| 179 | + logger.logAndRebufferResponse(CONFIG_KEY, Logger.Level.HEADERS, response, 100); |
| 180 | + |
| 181 | + // then |
| 182 | + assertNotNull(result.body(), "Response body object should exist"); |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + void nullBodyHandledCorrectlyAtHeadersLevel() throws Exception { |
| 187 | + // given |
| 188 | + TestLogger logger = new TestLogger(); |
| 189 | + Response response = |
| 190 | + Response.builder() |
| 191 | + .status(200) |
| 192 | + .reason("OK") |
| 193 | + .request( |
| 194 | + Request.create( |
| 195 | + HttpMethod.HEAD, "/api/resource", Collections.emptyMap(), null, Util.UTF_8)) |
| 196 | + .headers(Collections.<String, Collection<String>>emptyMap()) |
| 197 | + .body((byte[]) null) |
| 198 | + .build(); |
| 199 | + |
| 200 | + // when |
| 201 | + Response result = |
| 202 | + logger.logAndRebufferResponse(CONFIG_KEY, Logger.Level.HEADERS, response, 100); |
| 203 | + |
| 204 | + // then |
| 205 | + assertNull(result.body(), "Body should remain null"); |
| 206 | + } |
| 207 | +} |
0 commit comments