Skip to content

Commit

Permalink
Fixed OkHttp client redirect check when not following redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr committed May 21, 2024
1 parent 49b8f62 commit f547ad0
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@
class GruOkHttpResponse implements Client.Response {

private final Response response;
private final boolean followRedirects;

public GruOkHttpResponse(Response response) {
public GruOkHttpResponse(Response response, boolean followRedirects) {
this.response = response;
this.followRedirects = followRedirects;
}

@Override
public int getStatus() {
Response priorResponse = response.priorResponse();

if (priorResponse != null && priorResponse.isRedirect()) {
if (followRedirects && priorResponse != null && priorResponse.isRedirect()) {
return priorResponse.code();
}

Expand All @@ -49,7 +51,7 @@ public int getStatus() {

@Override
public List<String> getHeaders(String name) {
if (response.priorResponse() != null && response.priorResponse().isRedirect()) {
if (followRedirects && response.priorResponse() != null && response.priorResponse().isRedirect()) {
return response.priorResponse().headers(name);
}
return response.headers(name);
Expand All @@ -67,13 +69,16 @@ public String getText() {

@Override
public String getRedirectUrl() {
Response priorResponse = response.priorResponse();
return priorResponse == null ? null : priorResponse.header("Location");
if (followRedirects) {
Response priorResponse = response.priorResponse();
return priorResponse == null ? null : priorResponse.header("Location");
}
return response.header("Location");
}

@Override
public List<Cookie> getCookies() {
if (response.priorResponse() != null && response.priorResponse().isRedirect()) {
if (followRedirects && response.priorResponse() != null && response.priorResponse().isRedirect()) {
return Cookie.parseAll(response.priorResponse().headers("Set-Cookie"));
}
return Client.Response.super.getCookies();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void reset() {
public GruContext run(Squad squad, GruContext context) {
try {
okhttp3.Response response = httpClient.newCall(request.buildOkHttpRequest()).execute();
this.response = new GruOkHttpResponse(response);
this.response = new GruOkHttpResponse(response, httpClient.followRedirects());
return context.withResult(response);
} catch (IOException e) {
throw new AssertionError("Failed to execute request " + request, e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2018-2024 Agorapulse.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.agorapulse.gru.okhttp

import com.agorapulse.gru.Gru
import com.stehno.ersatz.ContentType
import com.stehno.ersatz.ErsatzServer
import spock.lang.AutoCleanup
import spock.lang.Shared
import spock.lang.Specification

class HttpNotFollowRedirectSpec extends Specification {

@AutoCleanup @Shared ErsatzServer server = new ErsatzServer({
autoStart(true)
})

@Shared Gru gru = Gru.create(OkHttp.create(this) {
it.followRedirects(false)
})

void setup() {
server.expectations {
get('/test') {
responds()
.cookie('Test-Cookie', 'true')
.header('Location', '/test/1')
.code(303)
.body('')

}
get('/test/1') {
responds()
.code(200)
.body('{"message":"OK"}', ContentType.APPLICATION_JSON)
}
}

gru.prepare(server.httpUrl('/'))
}

void 'expect cookie from original response when redirecting'() {
expect:
gru.test {
get('/test')
expect {
status SEE_OTHER
cookie 'Test-Cookie', 'true'
}
}
}

void 'expect header from original response when redirecting'() {
expect:
gru.test {
get('/test')
expect {
status SEE_OTHER
header 'Location', '/test/1'
}
}
}
}

0 comments on commit f547ad0

Please sign in to comment.