Skip to content

Commit

Permalink
Fix/#145 query parameter (#91)
Browse files Browse the repository at this point in the history
* Added test case for array query params

* Merge branch 'master' of https://github.com/networknt/light-rest-4j into fix/#145-query-parameter

# Conflicts:
#	openapi-validator/src/test/java/com/networknt/openapi/ValidatorHandlerTest.java

* Shut down server after testing
  • Loading branch information
jiachen1120 authored and stevehu committed May 17, 2019
1 parent 0693558 commit 04b200a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.Methods;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -59,6 +60,19 @@ public void setUp() {
}
}

@AfterClass
public static void tearDown() throws Exception {
if(server != null) {
try {
Thread.sleep(100);
} catch (InterruptedException ignored) {

}
server.stop();
logger.info("The server is stopped.");
}
}

@Test
public void testValidateResponseContentWithExchange() throws InterruptedException, ClientException, URISyntaxException, TimeoutException, ExecutionException {
ClientRequest clientRequest = new ClientRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,14 @@ RoutingHandler getPetStoreHandler() {
.add(Methods.POST, "/v1/pets", exchange -> exchange.getResponseSender().send("addPet"))
.add(Methods.GET, "/v1/pets/{petId}", exchange -> exchange.getResponseSender().send("getPetById"))
.add(Methods.DELETE, "/v1/pets/{petId}", exchange -> exchange.getResponseSender().send("deletePetById"))
.add(Methods.GET, "/v1/pets", exchange -> exchange.getResponseSender().send("getPets"))
.add(Methods.GET, "/v1/todoItems", forwardHandler);
.add(Methods.GET, "/v1/todoItems", forwardHandler)
.add(Methods.GET, "/v1/pets", exchange -> {
if (exchange.getQueryParameters() != null && exchange.getQueryParameters().containsKey("arr")) {
exchange.getResponseSender().send("getPets" + ", the query parameter = " + exchange.getQueryParameters() + ", length = " + exchange.getQueryParameters().get("arr").size());
} else {
exchange.getResponseSender().send("getPets");
}
});
}

@Test
Expand Down Expand Up @@ -500,6 +506,36 @@ public void testIssue57() throws Exception {
Assert.assertEquals(200, statusCode);
}

@Test
public void testQueryParameterArray() throws Exception {
final Http2Client client = Http2Client.getInstance();
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
final AtomicReference<ClientResponse> reference = new AtomicReference<>();
try {
ClientRequest request = new ClientRequest().setPath("/v1/pets?arr=1&arr=2&arr=3").setMethod(Methods.GET);
request.getRequestHeaders().put(Headers.HOST, "localhost");
request.getRequestHeaders().put(new HttpString("accessId"), "accessId");
request.getRequestHeaders().put(new HttpString("requestId"), "requestId");
connection.sendRequest(request, client.createClientCallback(reference, latch));
latch.await();
} catch (Exception e) {
logger.error("Exception: ", e);
throw new ClientException(e);
} finally {
IoUtils.safeClose(connection);
}
int statusCode = reference.get().getResponseCode();
String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
Assert.assertEquals(200, statusCode);
Assert.assertEquals("getPets, the query parameter = {arr=[1, 2, 3]}, length = 3", body);
}

@Test
public void testResponseContentValidationWithError() throws ClientException, URISyntaxException, ExecutionException, InterruptedException, TimeoutException {
ClientRequest clientRequest = new ClientRequest();
Expand Down
8 changes: 8 additions & 0 deletions openapi-validator/src/test/resources/config/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,16 @@ paths:
- name: all
in: query
description: Retrieve all for defect 57
required: false
schema:
type: boolean
- name: arr
in: query
description: Test array as query params
required: false
schema:
type: array
items: integer
security:
- petstore_auth:
- 'read:pets'
Expand Down

0 comments on commit 04b200a

Please sign in to comment.