Search in sources :

Example 6 with Header

use of okhttp3.internal.http2.Header in project Pokemap by omkarmoghe.

the class NianticManager method loginPTC.

private void loginPTC(final String username, final String password, NianticService.LoginValues values, final LoginListener loginListener) {
    HttpUrl url = HttpUrl.parse(LOGIN_URL).newBuilder().addQueryParameter("lt", values.getLt()).addQueryParameter("execution", values.getExecution()).addQueryParameter("_eventId", "submit").addQueryParameter("username", username).addQueryParameter("password", password).build();
    OkHttpClient client = mClient.newBuilder().followRedirects(false).followSslRedirects(false).build();
    NianticService service = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).client(client).build().create(NianticService.class);
    Callback<NianticService.LoginResponse> loginCallback = new Callback<NianticService.LoginResponse>() {

        @Override
        public void onResponse(Call<NianticService.LoginResponse> call, Response<NianticService.LoginResponse> response) {
            String location = response.headers().get("location");
            if (location != null && location.split("ticket=").length > 0) {
                String ticket = location.split("ticket=")[1];
                requestToken(ticket, loginListener);
            } else {
                Log.e(TAG, "PTC login failed via loginPTC(). There was no location header in response.");
                loginListener.authFailed("Pokemon Trainer Club Login Failed");
            }
        }

        @Override
        public void onFailure(Call<NianticService.LoginResponse> call, Throwable t) {
            t.printStackTrace();
            Log.e(TAG, "PTC login failed via loginPTC(). loginCallback.onFailure() threw: " + t.getMessage());
            loginListener.authFailed("Pokemon Trainer Club Login Failed");
        }
    };
    Call<NianticService.LoginResponse> call = service.login(url.toString());
    call.enqueue(loginCallback);
}
Also used : Call(retrofit2.Call) OkHttpClient(okhttp3.OkHttpClient) HttpUrl(okhttp3.HttpUrl) Response(retrofit2.Response) Retrofit(retrofit2.Retrofit) Callback(retrofit2.Callback)

Example 7 with Header

use of okhttp3.internal.http2.Header in project scribejava by scribejava.

the class OkHttpHttpClient method createCall.

private Call createCall(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, BodyType bodyType, Object bodyContents) {
    final Request.Builder requestBuilder = new Request.Builder();
    requestBuilder.url(completeUrl);
    final String method = httpVerb.name();
    // prepare body
    final RequestBody body;
    if (bodyContents != null && HttpMethod.permitsRequestBody(method)) {
        final MediaType mediaType = headers.containsKey(CONTENT_TYPE) ? MediaType.parse(headers.get(CONTENT_TYPE)) : DEFAULT_CONTENT_TYPE_MEDIA_TYPE;
        body = bodyType.createBody(mediaType, bodyContents);
    } else {
        body = null;
    }
    // fill HTTP method and body
    requestBuilder.method(method, body);
    // fill headers
    for (Map.Entry<String, String> header : headers.entrySet()) {
        requestBuilder.addHeader(header.getKey(), header.getValue());
    }
    if (userAgent != null) {
        requestBuilder.header(OAuthConstants.USER_AGENT_HEADER_NAME, userAgent);
    }
    // create a new call
    return client.newCall(requestBuilder.build());
}
Also used : Request(okhttp3.Request) OAuthRequest(com.github.scribejava.core.model.OAuthRequest) MediaType(okhttp3.MediaType) HashMap(java.util.HashMap) Map(java.util.Map) RequestBody(okhttp3.RequestBody)

Example 8 with Header

use of okhttp3.internal.http2.Header in project spring-framework by spring-projects.

the class WebClientIntegrationTests method plainText.

@Test
public void plainText() throws Exception {
    this.server.enqueue(new MockResponse().setBody("Hello Spring!"));
    Mono<String> result = this.webClient.get().uri("/greeting?name=Spring").header("X-Test-Header", "testvalue").exchange().then(response -> response.bodyToMono(String.class));
    StepVerifier.create(result).expectNext("Hello Spring!").expectComplete().verify(Duration.ofSeconds(3));
    RecordedRequest recordedRequest = server.takeRequest();
    Assert.assertEquals(1, server.getRequestCount());
    Assert.assertEquals("testvalue", recordedRequest.getHeader("X-Test-Header"));
    Assert.assertEquals("*/*", recordedRequest.getHeader(HttpHeaders.ACCEPT));
    Assert.assertEquals("/greeting?name=Spring", recordedRequest.getPath());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 9 with Header

use of okhttp3.internal.http2.Header in project spring-framework by spring-projects.

the class WebClientIntegrationTests method buildFilter.

@Test
public void buildFilter() throws Exception {
    this.server.enqueue(new MockResponse().setHeader("Content-Type", "text/plain").setBody("Hello Spring!"));
    WebClient filteredClient = this.webClient.filter((request, next) -> {
        ClientRequest filteredRequest = ClientRequest.from(request).header("foo", "bar").build();
        return next.exchange(filteredRequest);
    });
    Mono<String> result = filteredClient.get().uri("/greeting?name=Spring").exchange().then(response -> response.bodyToMono(String.class));
    StepVerifier.create(result).expectNext("Hello Spring!").expectComplete().verify(Duration.ofSeconds(3));
    RecordedRequest recordedRequest = server.takeRequest();
    Assert.assertEquals(1, server.getRequestCount());
    Assert.assertEquals("bar", recordedRequest.getHeader("foo"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 10 with Header

use of okhttp3.internal.http2.Header in project spring-framework by spring-projects.

the class WebClientIntegrationTests method filter.

@Test
public void filter() throws Exception {
    this.server.enqueue(new MockResponse().setHeader("Content-Type", "text/plain").setBody("Hello Spring!"));
    WebClient filteredClient = this.webClient.filter((request, next) -> {
        ClientRequest filteredRequest = ClientRequest.from(request).header("foo", "bar").build();
        return next.exchange(filteredRequest);
    });
    Mono<String> result = filteredClient.get().uri("/greeting?name=Spring").exchange().then(response -> response.bodyToMono(String.class));
    StepVerifier.create(result).expectNext("Hello Spring!").expectComplete().verify(Duration.ofSeconds(3));
    RecordedRequest recordedRequest = server.takeRequest();
    Assert.assertEquals(1, server.getRequestCount());
    Assert.assertEquals("bar", recordedRequest.getHeader("foo"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)88 MockResponse (okhttp3.mockwebserver.MockResponse)82 Request (okhttp3.Request)70 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)56 Response (okhttp3.Response)52 IOException (java.io.IOException)50 RequestBody (okhttp3.RequestBody)24 Call (okhttp3.Call)20 OkHttpClient (okhttp3.OkHttpClient)20 Callback (okhttp3.Callback)19 Interceptor (okhttp3.Interceptor)12 FormBody (okhttp3.FormBody)11 ResponseBody (okhttp3.ResponseBody)11 ArrayList (java.util.ArrayList)10 Map (java.util.Map)10 Headers (okhttp3.Headers)10 List (java.util.List)9 JSONObject (org.json.JSONObject)7 File (java.io.File)6 HashMap (java.util.HashMap)6