Skip to content

Commit

Permalink
[plugin-web-app-to-rest-api] Separate by engine steps setting HTTP co…
Browse files Browse the repository at this point in the history
…okie to browser (#5616)
  • Loading branch information
valfirst authored Dec 26, 2024
1 parent d60d3b4 commit 314451a
Show file tree
Hide file tree
Showing 25 changed files with 471 additions and 314 deletions.
19 changes: 10 additions & 9 deletions docs/modules/plugins/pages/plugin-web-app-to-rest-api.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ Then SSL rating for URL `https://www.google.com` is equal to `B`

==== Set browser cookies to the HTTP context

Set current browser cookies to the HTTP client for future executing HTTP requests.
Sets the current browser cookies to the HTTP context to use them in further HTTP requests.

[source,gherkin]
----
Expand All @@ -496,12 +496,11 @@ When I execute HTTP GET request for resource with relative URL `/get`

==== Set HTTP cookies to the browser

Set current cookies from the previous HTTP calls to the browser.
Make sure that expected cookies from HTTP calls matches with web browser opened page domain.
The actions performed by the step:
Sets the current cookies from the preceding HTTP calls to the current browser instance.
After adding the cookies the opened page is refreshed (this is required to apply the new cookies).

* add the cookies;
* refresh the current page (this action is required to apply the changes in cookies).
NOTE: If Selenium engine is used, ensure that the expected cookies from HTTP calls match the domain of the page opened
in the web browser.

[source,gherkin]
----
Expand All @@ -518,9 +517,11 @@ When I set HTTP context cookies to browser

==== Set HTTP cookies to the browser without applying changes

Set current cookies from the previous HTTP calls to the browser, but does not apply the changes in cookies instantly.
The current page must be refreshed or the navigation must be performed to apply the cookie changes.
Make sure that expected cookies from HTTP calls matches with web browser opened page domain.
Sets the current cookies from the preceding HTTP calls to the current browser instance, but does not apply the changes
in cookies instantly. The current page must be refreshed, or navigation must be performed to apply the cookie changes.

NOTE: If Selenium engine is used, ensure that the expected cookies from HTTP calls match the domain of the page opened
in the web browser.

[source,gherkin]
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,11 @@
* limitations under the License.
*/

package org.vividus.http;
package org.vividus.http.client;

import java.lang.reflect.Method;
import org.apache.hc.client5.http.cookie.CookieStore;

import org.jbehave.core.steps.NullStepMonitor;

public class CookieStepMonitor extends NullStepMonitor
public interface CookieStoreProvider
{
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();
}
}
CookieStore getCookieStore();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import org.apache.hc.client5.http.DnsResolver;
import org.apache.hc.client5.http.HttpRequestRetryStrategy;
import org.apache.hc.client5.http.cookie.CookieStore;
import org.apache.hc.client5.http.protocol.RedirectStrategy;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpRequestInterceptor;
Expand All @@ -49,7 +48,7 @@ public class HttpClientConfig
private int connectionRequestTimeout = -1;
private int connectTimeout = -1;
private int socketTimeout;
private CookieStore cookieStore;
private CookieStoreProvider cookieStoreProvider;
private boolean skipResponseEntity;
private DnsResolver dnsResolver;
private boolean circularRedirectsAllowed;
Expand Down Expand Up @@ -185,19 +184,19 @@ public void setSocketTimeout(int socketTimeout)
this.socketTimeout = socketTimeout;
}

public boolean hasCookieStore()
public boolean hasCookieStoreProvider()
{
return cookieStore != null;
return cookieStoreProvider != null;
}

public CookieStore getCookieStore()
public CookieStoreProvider getCookieStoreProvider()
{
return cookieStore;
return cookieStoreProvider;
}

public void setCookieStore(CookieStore cookieStore)
public void setCookieStoreProvider(CookieStoreProvider cookieStoreProvider)
{
this.cookieStore = cookieStore;
this.cookieStoreProvider = cookieStoreProvider;
}

public boolean isSkipResponseEntity()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ public IHttpClient buildHttpClient(HttpClientConfig config) throws GeneralSecuri
HttpClientBuilder builder = WinHttpClients.custom();

builder.setDefaultHeaders(config.createHeaders());
if (config.hasCookieStore())
if (config.hasCookieStoreProvider())
{
builder.setDefaultCookieStore(config.getCookieStore());
builder.setDefaultCookieStore(config.getCookieStoreProvider().getCookieStore());
}

configureAuth(config, builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand All @@ -31,8 +32,6 @@

import org.apache.hc.client5.http.DnsResolver;
import org.apache.hc.client5.http.HttpRequestRetryStrategy;
import org.apache.hc.client5.http.cookie.BasicCookieStore;
import org.apache.hc.client5.http.cookie.CookieStore;
import org.apache.hc.client5.http.protocol.RedirectStrategy;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpRequestInterceptor;
Expand Down Expand Up @@ -215,24 +214,20 @@ void testGetAndSetSocketTimeout()
}

@Test
void testHasCookieStore()
void testDoesNotHaveCookieStoreProvider()
{
config.setCookieStore(new BasicCookieStore());
assertTrue(config.hasCookieStore());
}

@Test
void testDoesNotHaveCookieStore()
{
assertFalse(config.hasCookieStore());
assertFalse(config.hasCookieStoreProvider());
}

@Test
void testCookieStore()
{
CookieStore cookieStore = new BasicCookieStore();
config.setCookieStore(cookieStore);
assertEquals(cookieStore, config.getCookieStore());
var cookieStoreProvider = mock(CookieStoreProvider.class);
config.setCookieStoreProvider(cookieStoreProvider);
assertAll(
() -> assertTrue(config.hasCookieStoreProvider()),
() -> assertEquals(cookieStoreProvider, config.getCookieStoreProvider())
);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,10 @@ private PoolingHttpClientConnectionManagerBuilder testBuildHttpClientUsingConfig
config.setCircularRedirectsAllowed(circularRedirectsAllowed);
String cookieSpec = "cookieSpec";
config.setCookieSpec(cookieSpec);
var cookieStoreProvider = mock(CookieStoreProvider.class);
CookieStore cookieStore = new BasicCookieStore();
config.setCookieStore(cookieStore);
when(cookieStoreProvider.getCookieStore()).thenReturn(cookieStore);
config.setCookieStoreProvider(cookieStoreProvider);
HttpRequestRetryStrategy retryStrategy = mock();
config.setHttpRequestRetryStrategy(retryStrategy);
try (var connectionManagerBuilderStaticMock = mockStatic(PoolingHttpClientConnectionManagerBuilder.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,38 @@

package org.vividus.http;

import java.lang.reflect.Method;

import org.apache.hc.client5.http.cookie.BasicCookieStore;
import org.apache.hc.client5.http.cookie.CookieStore;
import org.jbehave.core.annotations.AfterScenario;
import org.jbehave.core.annotations.AfterStory;
import org.jbehave.core.steps.NullStepMonitor;
import org.vividus.http.client.CookieStoreProvider;
import org.vividus.http.client.ThreadedBasicCookieStore;

public class CookieStoreProvider
public class CookieStoreProviderImpl extends NullStepMonitor implements CookieStoreProvider
{
private final CookieStoreLevel cookieStoreLevel;
private final CookieStore cookieStore;

public CookieStoreProvider(CookieStoreLevel cookieStoreLevel)
public CookieStoreProviderImpl(CookieStoreLevel cookieStoreLevel)
{
this.cookieStoreLevel = cookieStoreLevel;
this.cookieStore =
cookieStoreLevel == CookieStoreLevel.GLOBAL ? new BasicCookieStore() : new ThreadedBasicCookieStore();
}

@Override
public CookieStore getCookieStore()
{
return cookieStore;
}

public void resetStepCookies()
@Override
public void afterPerforming(String step, boolean dryRun, Method method)
{
if (cookieStoreLevel == CookieStoreLevel.STEP)
if (!dryRun && cookieStoreLevel == CookieStoreLevel.STEP)
{
cookieStore.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vividus.context.VariableContext;
import org.vividus.http.CookieStoreProvider;
import org.vividus.http.client.CookieStoreProvider;
import org.vividus.softassert.ISoftAssert;
import org.vividus.variable.VariableScope;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<bean id="httpTestContext" class="org.vividus.http.HttpTestContext" />

Check failure on line 10 in vividus-plugin-rest-api/src/main/resources/vividus-plugin/spring.xml

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Incorrect constructor injection in XML Spring bean

No matching constructor found in class 'HttpTestContext'#treeend *** ** * ** *** |---------------------------|---|-----------| | **HttpTestContext(...):** | | **Bean:** | | TestContext testContext | | **???** |

<bean id="cookieStoreProvider" class="org.vividus.http.CookieStoreProvider">
<bean id="cookieStoreProvider" class="org.vividus.http.CookieStoreProviderImpl">

Check failure on line 12 in vividus-plugin-rest-api/src/main/resources/vividus-plugin/spring.xml

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Incorrect constructor injection in XML Spring bean

No matching constructor found in class 'CookieStoreProviderImpl'#treeend *** ** * ** *** |-----------------------------------|---|-----------| | **CookieStoreProviderImpl(...):** | | **Bean:** | | CookieStoreLevel cookieStoreLevel | | **???** |
<constructor-arg value="${rest-api.http.cookie-store-level}" />

Check warning on line 13 in vividus-plugin-rest-api/src/main/resources/vividus-plugin/spring.xml

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Unresolved placeholders configured in the Spring XML application context

Cannot resolve property key
</bean>

Expand All @@ -32,9 +32,7 @@
<property name="httpTestContext" ref="httpTestContext" />
</bean>
</property>
<property name="cookieStore">
<bean factory-method="getCookieStore" factory-bean="cookieStoreProvider" />
</property>
<property name="cookieStoreProvider" ref="cookieStoreProvider" />

Check failure on line 35 in vividus-plugin-rest-api/src/main/resources/vividus-plugin/spring.xml

View workflow job for this annotation

GitHub Actions / Qodana for JVM

Incorrect Spring Core XML-based application context

Cannot resolve property 'cookieStoreProvider'
<property name="httpResponseHandlers">
<list>
<ref bean="extendedHttpLoggingInterceptor" />
Expand Down Expand Up @@ -131,6 +129,4 @@
<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>

This file was deleted.

Loading

0 comments on commit 314451a

Please sign in to comment.