Skip to content

Commit

Permalink
[plugin-rest-api] Add ability to clean up cookies after each step (vi…
Browse files Browse the repository at this point in the history
  • Loading branch information
abudevich authored Sep 24, 2024
1 parent 4bb416b commit dc166e4
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 5 deletions.
14 changes: 14 additions & 0 deletions docs/modules/plugins/pages/plugin-rest-api.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ a|`true`
|`false`
|Enable logging of HTTP request/response headers and bodies (applied to the following content types only: `text/*`, `application/json`, `application/xml`)

|`rest-api.http.cookie-store-level`
a|`global` +
`story` +
`scenario` +
`step`
|`global`
a|Configure the level of cookie storage:

* `global` - cookies are stored globally and are accessible across the entire suite from all threads: cookies set in
one scenario are available and reused in all subsequent scenarios and stories.
* `story` - cookies are reused within the scope of a single story and are cleared upon its completion.
* `scenario` - cookies are reused within the scope of a single scenario and are cleared upon its completion.
* `step` - cookies are cleared after each executed step.

|===

See xref:configuration:tests-configuration.adoc#_http_configuration[HTTP configuration] for more fine-grained control over the HTTP interactions.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2019-2024 the original author or authors.
*
* 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 org.vividus.http;

import java.lang.reflect.Method;

import org.jbehave.core.steps.NullStepMonitor;

public class CookieStepMonitor extends NullStepMonitor
{
private final CookieStoreProvider cookieStoreProvider;

public CookieStepMonitor(CookieStoreProvider cookieStoreProvider)
{
this.cookieStoreProvider = cookieStoreProvider;
}

@Override
public void afterPerforming(String step, boolean dryRun, Method method)
{
if (!dryRun)
{
cookieStoreProvider.resetStepCookies();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2020 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,5 +20,6 @@ public enum CookieStoreLevel
{
GLOBAL,
STORY,
SCENARIO
SCENARIO,
STEP
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,6 +39,14 @@ public CookieStore getCookieStore()
return cookieStore;
}

public void resetStepCookies()
{
if (cookieStoreLevel == CookieStoreLevel.STEP)
{
cookieStore.clear();
}
}

@AfterScenario
public void resetScenarioCookies()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,6 @@
<bean class="org.vividus.http.expression.PkceExpressionProcessor" />
<bean class="org.vividus.http.expression.RemoveWrappingDoubleQuotesExpressionProcessor" />
<bean class="org.vividus.http.expression.UriExpressionProcessors" />

<bean class="org.vividus.http.CookieStepMonitor" />
</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2019-2024 the original author or authors.
*
* 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 org.vividus.http;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class CookieStepMonitorTests
{
@Mock private CookieStoreProvider cookieStoreProvider;
@InjectMocks private CookieStepMonitor cookieStepMonitor;

@Test
void shouldClearCookiesAfterStep()
{
cookieStepMonitor.afterPerforming(null, false, null);
verify(cookieStoreProvider).resetStepCookies();
}

@Test
void shouldNotClearCookiesAfterStep()
{
cookieStepMonitor.afterPerforming(null, true, null);
verifyNoInteractions(cookieStoreProvider);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 the original author or authors.
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,14 +35,24 @@ class CookieStoreProviderTests
@CsvSource({
"GLOBAL, org.apache.hc.client5.http.cookie.BasicCookieStore",
"STORY, org.vividus.http.client.ThreadedBasicCookieStore",
"SCENARIO, org.vividus.http.client.ThreadedBasicCookieStore"
"SCENARIO, org.vividus.http.client.ThreadedBasicCookieStore",
"STEP, org.vividus.http.client.ThreadedBasicCookieStore"
})
void shouldCreateCookieStore(CookieStoreLevel cookieStoreLevel, Class<?> clazz)
{
CookieStoreProvider cookieStoreProvider = new CookieStoreProvider(cookieStoreLevel);
assertEquals(clazz, cookieStoreProvider.getCookieStore().getClass());
}

@ParameterizedTest
@EnumSource(names = "STEP", mode = Mode.EXCLUDE)
void shouldNotClearCookieStoreAfterStep(CookieStoreLevel cookieStoreLevel)
{
CookieStoreProvider cookieStoreProvider = createProviderWithCookie(cookieStoreLevel);
cookieStoreProvider.resetStepCookies();
assertEquals(List.of(COOKIE), cookieStoreProvider.getCookieStore().getCookies());
}

@ParameterizedTest
@EnumSource(names = "SCENARIO", mode = Mode.EXCLUDE)
void shouldNotClearCookieStoreAfterScenario(CookieStoreLevel cookieStoreLevel)
Expand All @@ -61,6 +71,14 @@ void shouldNotClearCookieStoreAfterStory(CookieStoreLevel cookieStoreLevel)
assertEquals(List.of(COOKIE), cookieStoreProvider.getCookieStore().getCookies());
}

@Test
void shouldClearCookieStoreAfterStep()
{
CookieStoreProvider cookieStoreProvider = createProviderWithCookie(CookieStoreLevel.STEP);
cookieStoreProvider.resetStepCookies();
assertEquals(List.of(), cookieStoreProvider.getCookieStore().getCookies());
}

@Test
void shouldClearCookieStoreAfterScenario()
{
Expand Down

0 comments on commit dc166e4

Please sign in to comment.