@Test public void testQueryParameters() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (AsyncHttpClient client = asyncHttpClient()) { Future<Response> f = client.prepareGet("http://localhost:" + port1).addQueryParam("a", "1").addQueryParam("b", "2").execute(); Response resp = f.get(3, TimeUnit.SECONDS); assertNotNull(resp); assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK); assertEquals(resp.getHeader("a"), "1"); assertEquals(resp.getHeader("b"), "2"); } }
public void send(String token, String secret, String message, AsyncCompletionHandler<Response> handler) { asyncHttpClient .preparePost(TWITTER_UPDATE_STATUS_URL) .addQueryParam("status", message) .setSignatureCalculator(new OAuthSignatureCalculator(consumerKey, new RequestToken(token, secret))) .execute(handler); } }
@Test public void shouldReturn403WhenRequestingWithoutOauthToken() throws Exception { // Given // When final Response response = asyncHttpClient .prepareGet(baseUrl + "/api/hello") .addQueryParam("context", "mode") .addHeader(ACCEPT, MediaType.APPLICATION_JSON_UTF8_VALUE) .execute() .get(); // Then assertThat(response.getStatusCode(), is(403)); assertThat(response.getContentType(), is(MediaType.APPLICATION_JSON_UTF8_VALUE)); }
@Test public void shouldReturnHelloResponseWithValidOauthToken() throws Exception { // Given final String bearerToken = oAuthTestHelper.getBearerToken("hello.read"); // When final Response response = asyncHttpClient .prepareGet(baseUrl + "/api/hello") .addQueryParam("context", "mode") .addHeader(AUTHORIZATION, bearerToken) .addHeader(ACCEPT, MediaType.APPLICATION_JSON_UTF8_VALUE) .execute() .get(); // Then assertThat(response.getStatusCode(), is(200)); assertThat(response.getContentType(), is(MediaType.APPLICATION_JSON_UTF8_VALUE)); assertThat(response.getResponseBody(), is("{\"hello\": \"world\"}")); }
@Test public void shouldReturn403WhenRequestingWithInvalidOauthToken() throws Exception { // Given final String bearerToken = "someInvalidBearerToken"; // When final Response response = asyncHttpClient .prepareGet(baseUrl + "/api/hello") .addQueryParam("context", "mode") .addHeader(AUTHORIZATION, bearerToken) .addHeader(ACCEPT, MediaType.APPLICATION_JSON_UTF8_VALUE) .execute() .get(); // Then assertThat(response.getStatusCode(), is(403)); assertThat(response.getContentType(), is(MediaType.APPLICATION_JSON_UTF8_VALUE)); }
@Test public void shouldReturn403WhenRequestingWithInvalidScopeInOauthToken() throws Exception { // Given final String bearerToken = oAuthTestHelper.getBearerToken("hello.write"); // When final Response response = asyncHttpClient .prepareGet(baseUrl + "/api/hello") .addQueryParam("context", "mode") .addHeader(AUTHORIZATION, bearerToken) .addHeader(ACCEPT, MediaType.APPLICATION_JSON_UTF8_VALUE) .execute() .get(); // Then assertThat(response.getStatusCode(), is(403)); assertThat(response.getContentType(), is(MediaType.APPLICATION_JSON_UTF8_VALUE)); }