Skip to content

Commit

Permalink
Merge pull request #82 from agorapulse/bug/sc99874-fix-headers-and-co…
Browse files Browse the repository at this point in the history
…okies-in-redirection

Fix checking headers and cookies in response from redirection
  • Loading branch information
wololock committed Jun 7, 2023
2 parents b387955 + 0fe7038 commit 34e9297
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 1 deletion.
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ springBootVersion = 2.1.15.RELEASE
micronautVersion=2.4.0
micronautCompactVersion=1.3.7
kotlinVersion=1.6.21
ersatzVersion=1.9.0
undertowVersion=2.0.13.Final

4 changes: 4 additions & 0 deletions libs/gru-http/gru-http.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ dependencies {
api "com.squareup.okhttp3:okhttp:${okHttpVersion}"

compileOnly "org.codehaus.groovy:groovy:$groovyVersion"

testImplementation "com.stehno.ersatz:ersatz:$ersatzVersion"
testImplementation "io.undertow:undertow-core:$undertowVersion"
testImplementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.20.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.agorapulse.gru.http;

import com.agorapulse.gru.Client;
import com.agorapulse.gru.cookie.Cookie;
import okhttp3.Response;
import okhttp3.ResponseBody;

Expand Down Expand Up @@ -48,6 +49,9 @@ public int getStatus() {

@Override
public List<String> getHeaders(String name) {
if (response.priorResponse() != null && response.priorResponse().isRedirect()) {
return response.priorResponse().headers(name);
}
return response.headers(name);
}

Expand All @@ -67,4 +71,12 @@ public String getRedirectUrl() {
return priorResponse == null ? null : priorResponse.header("Location");
}

@Override
public List<Cookie> getCookies() {
if (response.priorResponse() != null &&
response.priorResponse().isRedirect()) {
return Cookie.parseAll(response.priorResponse().headers("Set-Cookie"));
}
return Client.Response.super.getCookies();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2018-2023 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.http

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 HttpRedirectSpec extends Specification {

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

@Shared Gru gru = Gru.create(Http.create(this))

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'
}
}
}
}
1 change: 1 addition & 0 deletions libs/gru/gru.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dependencies {
api group: 'org.jsoup', name: 'jsoup', version: jsoupVersion

implementation 'com.fasterxml.jackson.core:jackson-databind:2.9.6'
implementation 'org.hamcrest:hamcrest:2.2'

compileOnly "org.codehaus.groovy:groovy:$groovyVersion"
compileOnly 'org.jetbrains:annotations:13.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,15 @@ class DefaultTestDefinitionBuilderSpec extends Specification {
String expectedHeader = "X-Foo"

Client.Request request = Mock(Client.Request)
Client.Response response = Mock(Client.Response)
Client.Response response = Mock(Client.Response) {
getStatus() >> 200
}

Client client = Mock(Client) {
getInitialSquad() >> []
getRequest() >> request
getResponse() >> response
run(_,_) >> { args -> args[1]}
}

response.getHeaders(expectedHeader) >> ["Bar", "Baz"]
Expand Down

0 comments on commit 34e9297

Please sign in to comment.