Skip to content

Commit

Permalink
function to copy a request
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Apr 24, 2024
1 parent 3d84fc6 commit ccce8f7
Show file tree
Hide file tree
Showing 9 changed files with 336 additions and 14 deletions.
51 changes: 38 additions & 13 deletions unirest-bdd-tests/src/test/java/BehaviorTests/AsPagedTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/**
* The MIT License
*
* <p>
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
*
* <p>
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* <p>
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Expand Down Expand Up @@ -41,38 +41,63 @@ class AsPagedTest extends BddTest {
void canFollowPaging() {
MockServer.expectedPages(10);

PagedList<RequestCapture> result = Unirest.get(MockServer.PAGED)
PagedList<RequestCapture> result = Unirest.get(MockServer.PAGED)
.header("x-header", "h-value")
.asPaged(
r -> r.asObject(RequestCapture.class),
r -> r.getHeaders().getFirst("nextPage")
);

assertEquals(10, result.size());
assertThat(result)
.hasSize(10)
.allMatch(r -> {
r.getBody().assertHeader("x-header", "h-value");
return true;
});
}

@Test
void canFollowPagingForPost() {
MockServer.expectedPages(10);

PagedList<RequestCapture> result = Unirest.post(MockServer.PAGED)
.body("Hi Mom")
.asPaged(
r -> r.asObject(RequestCapture.class),
r -> r.getHeaders().getFirst("nextPage")
);

assertThat(result)
.hasSize(10)
.allMatch(r -> {
r.getBody().assertBody("Hi Mom");
return true;
});
}

@Test
void canCapturePagesAsStrings() {
MockServer.expectedPages(10);

PagedList<String> result = Unirest.get(MockServer.PAGED)
PagedList<String> result = Unirest.get(MockServer.PAGED)
.asPaged(
r -> r.asString(),
r -> r.getHeaders().getFirst("nextPage")
);

assertEquals(10, result.size());
assertThat(result).hasSize(10);

}

@Test
void willReturnOnePageIfthereWasNoPaging() {

PagedList<RequestCapture> result = Unirest.get(MockServer.PAGED)
PagedList<RequestCapture> result = Unirest.get(MockServer.PAGED)
.asPaged(
r -> r.asObject(RequestCapture.class),
r -> null
);

assertEquals(1, result.size());
assertThat(result).hasSize(1);
}

@Test
Expand All @@ -82,8 +107,8 @@ void asPagedWithRedirects() {
var responses = Unirest.get(MockServer.REDIRECT)
.asPaged(HttpRequest::asString,
response -> List.of(301, 302).contains(response.getStatus())
? "http://localhost:4567/" + response.getHeaders().getFirst("Location")
: null);
? "http://localhost:4567/" + response.getHeaders().getFirst("Location")
: null);


assertThat(responses).hasSize(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public static void reset() {
app.get("/proxy", MockServer::proxiedResponse);
app.get("/binary", MockServer::file);
app.get("/paged", MockServer::paged);
app.post("/paged", MockServer::paged);
app.post("/raw", MockServer::echo);
app.get("/error", MockServer::error);
app.get("/hello", MockServer::helloWOrld);
Expand Down
9 changes: 8 additions & 1 deletion unirest/src/main/java/kong/unirest/core/BaseRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ abstract class BaseRequest<R extends HttpRequest> implements HttpRequest<R> {
this.connectTimeout = httpRequest.connectTimeout;
this.objectMapper = httpRequest.objectMapper;
this.version = httpRequest.version;
this.downloadMonitor = httpRequest.downloadMonitor;
}

BaseRequest(Config config, HttpMethod method, String url) {
Expand Down Expand Up @@ -180,6 +181,10 @@ public R downloadMonitor(ProgressMonitor monitor) {
return (R) this;
}

public ProgressMonitor getDownloadMonitor(){
return this.downloadMonitor;
}

@Override
public R version(HttpClient.Version value) {
this.version = value;
Expand Down Expand Up @@ -339,13 +344,15 @@ public <T> PagedList<T> asPaged(Function<HttpRequest, HttpResponse> mappingFunct
String nextLink = this.getUrl();
do {
this.url = new Path(nextLink, config.getDefaultBaseUrl());
HttpResponse<T> next = mappingFunction.apply(this);
BaseRequest<R> t = RequestFactory.copy(this);
HttpResponse<T> next = mappingFunction.apply(t);
all.add(next);
nextLink = linkExtractor.apply(next);
} while (!Util.isNullOrEmpty(nextLink));
return all;
}


private <E> HttpResponse<E> request(Function<RawResponse, HttpResponse<E>> transformer, Class<?> resultType){
HttpResponse<E> response = config.getClient().request(this, transformer, resultType);

Expand Down
5 changes: 5 additions & 0 deletions unirest/src/main/java/kong/unirest/core/HttpRequestBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public HttpRequestBody(Config config, HttpMethod method, String url) {
super(config, method, url);
}

public HttpRequestBody(HttpRequestBody baseRequest) {
super(baseRequest);
this.charSet = baseRequest.getCharset();
}

@Override
public MultipartBody field(String name, Collection<?> value) {
return new HttpRequestMultiPart(this).field(name, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ class HttpRequestMultiPart extends BaseRequest<MultipartBody> implements Multipa
this.charSet = httpRequest.getCharset();
}

HttpRequestMultiPart(HttpRequestMultiPart httpRequest) {
super(httpRequest);
this.charSet = httpRequest.getCharset();
this.parameters = httpRequest.parameters;
this.forceMulti = httpRequest.forceMulti;
this.monitor = httpRequest.monitor;
this.boundary = httpRequest.boundary;
}

@Override
public MultipartBody field(String name, String value) {
addPart(new ParamPart(name, value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class HttpRequestNoBody extends BaseRequest<GetRequest> implements GetRequest {
super(config, method, url);
}

HttpRequestNoBody(HttpRequestNoBody baseRequest) {
super(baseRequest);
}

@Override
public Optional<Body> getBody() {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ class HttpRequestUniBody extends BaseRequest<RequestBodyEntity> implements Reque
this.charSet = httpRequest.getCharset();
}

HttpRequestUniBody(HttpRequestUniBody httpRequest) {
super(httpRequest);
this.charSet = httpRequest.getCharset();
this.body = httpRequest.body;
this.monitor = httpRequest.monitor;
}

@Override
public RequestBodyEntity body(JsonNode jsonBody) {
return body(jsonBody.toString());
Expand Down
20 changes: 20 additions & 0 deletions unirest/src/main/java/kong/unirest/core/RequestFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package kong.unirest.core;

class RequestFactory {
public static <R extends BaseRequest> R copy(HttpRequest baseRequest) {
if(baseRequest instanceof HttpRequestNoBody){
return (R) new HttpRequestNoBody((HttpRequestNoBody)baseRequest);
}
if(baseRequest instanceof HttpRequestBody){
return (R) new HttpRequestBody((HttpRequestBody)baseRequest);
}
if(baseRequest instanceof HttpRequestUniBody){
return (R) new HttpRequestUniBody((HttpRequestUniBody)baseRequest);
}
if(baseRequest instanceof HttpRequestMultiPart) {
return (R) new HttpRequestMultiPart((HttpRequestMultiPart)baseRequest);
}

throw new UnirestException("Cannot find matching type: " + baseRequest.getClass());
}
}
Loading

0 comments on commit ccce8f7

Please sign in to comment.