| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,7 @@ | |||
| 1 | 1 | ## 3.13.0 | |
| 2 | 2 | * Support InputStreams as bodies. | |
| 3 | 3 | * Support ProgressMonitors for InputStream bodies | |
| 4 | + * Unirest-Mocks now includes a MockResponse<T> and a MockConfig for use independent of the MockClient | ||
| 4 | 5 | ||
| 5 | 6 | ## 3.12.0 | |
| 6 | 7 | * Bump GSON to 2.8.8 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,43 @@ | |||
| 1 | + /** | ||
| 2 | + * The MIT License | ||
| 3 | + * | ||
| 4 | + * Copyright for portions of unirest-java are held by Kong Inc (c) 2013. | ||
| 5 | + * | ||
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining | ||
| 7 | + * a copy of this software and associated documentation files (the | ||
| 8 | + * "Software"), to deal in the Software without restriction, including | ||
| 9 | + * without limitation the rights to use, copy, modify, merge, publish, | ||
| 10 | + * distribute, sublicense, and/or sell copies of the Software, and to | ||
| 11 | + * permit persons to whom the Software is furnished to do so, subject to | ||
| 12 | + * the following conditions: | ||
| 13 | + * | ||
| 14 | + * The above copyright notice and this permission notice shall be | ||
| 15 | + * included in all copies or substantial portions of the Software. | ||
| 16 | + * | ||
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| 18 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
| 19 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
| 20 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
| 21 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
| 22 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
| 23 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 24 | + */ | ||
| 25 | + | ||
| 26 | + package kong.unirest; | ||
| 27 | + | ||
| 28 | + /** | ||
| 29 | + * A Mock Config that will not actually start up any real clients. | ||
| 30 | + */ | ||
| 31 | + public class MockConfig extends Config { | ||
| 32 | + private MockClient client = new MockClient(() -> this); | ||
| 33 | + | ||
| 34 | + @Override | ||
| 35 | + public Client getClient() { | ||
| 36 | + return client; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + @Override | ||
| 40 | + public AsyncClient getAsyncClient() { | ||
| 41 | + return client; | ||
| 42 | + } | ||
| 43 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,172 @@ | |||
| 1 | + /** | ||
| 2 | + * The MIT License | ||
| 3 | + * | ||
| 4 | + * Copyright for portions of unirest-java are held by Kong Inc (c) 2013. | ||
| 5 | + * | ||
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining | ||
| 7 | + * a copy of this software and associated documentation files (the | ||
| 8 | + * "Software"), to deal in the Software without restriction, including | ||
| 9 | + * without limitation the rights to use, copy, modify, merge, publish, | ||
| 10 | + * distribute, sublicense, and/or sell copies of the Software, and to | ||
| 11 | + * permit persons to whom the Software is furnished to do so, subject to | ||
| 12 | + * the following conditions: | ||
| 13 | + * | ||
| 14 | + * The above copyright notice and this permission notice shall be | ||
| 15 | + * included in all copies or substantial portions of the Software. | ||
| 16 | + * | ||
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| 18 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
| 19 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
| 20 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
| 21 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
| 22 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
| 23 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 24 | + */ | ||
| 25 | + | ||
| 26 | + package kong.unirest; | ||
| 27 | + | ||
| 28 | + import java.util.Optional; | ||
| 29 | + import java.util.function.Consumer; | ||
| 30 | + | ||
| 31 | + /** | ||
| 32 | + * A Mock Response that can be used in testing. | ||
| 33 | + * @param <T> the body type | ||
| 34 | + */ | ||
| 35 | + public class MockResponse<T> extends BaseResponse<T> { | ||
| 36 | + private final MockConfig mockConfig; | ||
| 37 | + private Optional<UnirestParsingException> ex = Optional.empty(); | ||
| 38 | + private final T body; | ||
| 39 | + | ||
| 40 | + /** | ||
| 41 | + * Construct a mock Response | ||
| 42 | + * @param status the status of the response | ||
| 43 | + * @param statusText the status text | ||
| 44 | + * @param body the body | ||
| 45 | + */ | ||
| 46 | + public MockResponse(int status, String statusText, T body){ | ||
| 47 | + this(status, statusText, body, new MockConfig()); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + /** | ||
| 51 | + * Construct a mock Response | ||
| 52 | + * @param status the status of the response | ||
| 53 | + * @param statusText the status text | ||
| 54 | + * @param body the body | ||
| 55 | + * @param config a mockConfig for post-processing options | ||
| 56 | + */ | ||
| 57 | + public MockResponse(int status, String statusText, T body, MockConfig config){ | ||
| 58 | + super(new MockRawResponse( | ||
| 59 | + "", new Headers(), status, statusText, config | ||
| 60 | + )); | ||
| 61 | + this.mockConfig = config; | ||
| 62 | + this.body = body; | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + /** | ||
| 66 | + * Construct a simple successful (200 ok) response with a body | ||
| 67 | + * @param body the body | ||
| 68 | + * @param <T> the type of body | ||
| 69 | + * @return a MockResponse; | ||
| 70 | + */ | ||
| 71 | + public static <T> MockResponse<T> ok(T body) { | ||
| 72 | + return new MockResponse<>(200, "ok", body); | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + /** | ||
| 76 | + * Construct a simple failed (400 bad request) response with a body | ||
| 77 | + * @param body the body | ||
| 78 | + * @param <T> the type of body | ||
| 79 | + * @return a MockResponse; | ||
| 80 | + */ | ||
| 81 | + public static <T> MockResponse<T> bad(T body) { | ||
| 82 | + return new MockResponse<>(400, "bad request", body); | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + /** | ||
| 86 | + * Construct a response with a status and body. The status text is just the string of the status | ||
| 87 | + * @param status the status | ||
| 88 | + * @param body the body | ||
| 89 | + * @param <T> the type of body | ||
| 90 | + * @return a MockResponse; | ||
| 91 | + */ | ||
| 92 | + public static <T> MockResponse<T> of(int status, T body) { | ||
| 93 | + return new MockResponse<>(status, String.valueOf(status), body); | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + /** | ||
| 97 | + * Construct a response with a status and body. The status text is just the string of the status | ||
| 98 | + * @param status the status | ||
| 99 | + * @param statusText the status text | ||
| 100 | + * @param body the body | ||
| 101 | + * @param <T> the type of body | ||
| 102 | + * @return a MockResponse; | ||
| 103 | + */ | ||
| 104 | + public static <T> MockResponse<T> of(int status, String statusText, T body) { | ||
| 105 | + return new MockResponse<>(status, statusText, body); | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + /** | ||
| 109 | + * get the MockConfig for this MockResponse | ||
| 110 | + * @return the config | ||
| 111 | + */ | ||
| 112 | + public MockConfig config(){ | ||
| 113 | + return mockConfig; | ||
| 114 | + } | ||
| 115 | + | ||
| 116 | + | ||
| 117 | + @Override | ||
| 118 | + public T getBody() { | ||
| 119 | + return body; | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + @Override | ||
| 123 | + protected String getRawBody() { | ||
| 124 | + return String.valueOf(body); | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + @Override | ||
| 128 | + public Optional<UnirestParsingException> getParsingError() { | ||
| 129 | + return ex; | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + /** | ||
| 133 | + * add a header value to the response | ||
| 134 | + * @param key the header key | ||
| 135 | + * @param value the header value | ||
| 136 | + * @return this MockResponse | ||
| 137 | + */ | ||
| 138 | + public MockResponse<T> withHeader(String key, String value) { | ||
| 139 | + getHeaders().add(key, value); | ||
| 140 | + return this; | ||
| 141 | + } | ||
| 142 | + | ||
| 143 | + /** | ||
| 144 | + * Flag that there was a post-processing parsing error with the object Mapper | ||
| 145 | + * this jams the body as a string into the parsing exception and sets a generic oops exception | ||
| 146 | + * @return this MockResponse | ||
| 147 | + */ | ||
| 148 | + public MockResponse<T> failedToParse() { | ||
| 149 | + return failedToParse(new Exception("oops"), String.valueOf(body)); | ||
| 150 | + } | ||
| 151 | + | ||
| 152 | + /** | ||
| 153 | + * Flag that there was a post-processing parsing error with the object Mapper | ||
| 154 | + * @param e the exception thrown | ||
| 155 | + * @param originalBody the original body before the object mapper got involved. | ||
| 156 | + * @return this MockResponse | ||
| 157 | + */ | ||
| 158 | + public MockResponse<T> failedToParse(Exception e, String originalBody) { | ||
| 159 | + ex = Optional.of(new UnirestParsingException(originalBody, e)); | ||
| 160 | + return this; | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | + /** | ||
| 164 | + * Set some options on the current MockConfig. | ||
| 165 | + * @param c a Consumer with options to set on the config | ||
| 166 | + * @return this MockResponse | ||
| 167 | + */ | ||
| 168 | + public MockResponse<T> withConfigOptions(Consumer<MockConfig> c) { | ||
| 169 | + c.accept(mockConfig); | ||
| 170 | + return this; | ||
| 171 | + } | ||
| 172 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,79 @@ | |||
| 1 | + /** | ||
| 2 | + * The MIT License | ||
| 3 | + * | ||
| 4 | + * Copyright for portions of unirest-java are held by Kong Inc (c) 2013. | ||
| 5 | + * | ||
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining | ||
| 7 | + * a copy of this software and associated documentation files (the | ||
| 8 | + * "Software"), to deal in the Software without restriction, including | ||
| 9 | + * without limitation the rights to use, copy, modify, merge, publish, | ||
| 10 | + * distribute, sublicense, and/or sell copies of the Software, and to | ||
| 11 | + * permit persons to whom the Software is furnished to do so, subject to | ||
| 12 | + * the following conditions: | ||
| 13 | + * | ||
| 14 | + * The above copyright notice and this permission notice shall be | ||
| 15 | + * included in all copies or substantial portions of the Software. | ||
| 16 | + * | ||
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
| 18 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
| 19 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
| 20 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
| 21 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
| 22 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
| 23 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 24 | + */ | ||
| 25 | + | ||
| 26 | + package kong.unirest; | ||
| 27 | + | ||
| 28 | + import org.junit.jupiter.api.Test; | ||
| 29 | + | ||
| 30 | + import static org.junit.jupiter.api.Assertions.*; | ||
| 31 | + | ||
| 32 | + class MockResponseTest { | ||
| 33 | + | ||
| 34 | + @Test | ||
| 35 | + void canCreateASuccessfulMockResponse() { | ||
| 36 | + HttpResponse<String> response = MockResponse.ok("Hi Mom"); | ||
| 37 | + assertEquals(200, response.getStatus()); | ||
| 38 | + assertEquals("Hi Mom", response.getBody()); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + @Test | ||
| 42 | + void canAddHeaders() { | ||
| 43 | + HttpResponse<String> response = MockResponse.ok("Hi Mom") | ||
| 44 | + .withHeader("Accept", "application/xml"); | ||
| 45 | + | ||
| 46 | + assertEquals("application/xml", response.getHeaders().getFirst("Accept")); | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + @Test | ||
| 50 | + void badRequestWithParsingException() { | ||
| 51 | + HttpResponse<String> response = MockResponse.bad("Hi Mom") | ||
| 52 | + .failedToParse(); | ||
| 53 | + | ||
| 54 | + assertEquals(400, response.getStatus()); | ||
| 55 | + assertEquals("Hi Mom", response.getParsingError().get().getOriginalBody()); | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + @Test | ||
| 59 | + void canSetConfigOptions() { | ||
| 60 | + HttpResponse<String> response = MockResponse.bad("Hi Mom") | ||
| 61 | + .withConfigOptions(c -> c.setObjectMapper(new ObjectMapper() { | ||
| 62 | + @Override | ||
| 63 | + public <T> T readValue(String value, Class<T> valueType) { | ||
| 64 | + return (T)new TransformedError(); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + @Override | ||
| 68 | + public String writeValue(Object value) { | ||
| 69 | + return null; | ||
| 70 | + } | ||
| 71 | + })); | ||
| 72 | + | ||
| 73 | + assertEquals("Transformed!", response.mapError(TransformedError.class).message); | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + class TransformedError { | ||
| 77 | + String message = "Transformed!"; | ||
| 78 | + } | ||
| 79 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments