How to use getCookiesMap method of com.consol.citrus.http.message.HttpMessage class

Best Citrus code snippet using com.consol.citrus.http.message.HttpMessage.getCookiesMap

Source:HttpMessage.java Github

copy

Full Screen

...86 * @param message the message to extract the cookies from87 */88 private void copyCookies(final Message message) {89 if (message instanceof HttpMessage) {90 this.cookies.putAll(((HttpMessage) message).getCookiesMap());91 }92 }93 /**94 * Sets the Http request method header.95 *96 * @param method The Http method header to use97 * @return The altered HttpMessage98 */99 public HttpMessage method(final HttpMethod method) {100 setHeader(HttpMessageHeaders.HTTP_REQUEST_METHOD, method.name());101 return this;102 }103 /**104 * Sets the Http version header.105 *106 * @param version The http version header value to use107 * @return The altered HttpMessage108 */109 public HttpMessage version(final String version) {110 setHeader(HttpMessageHeaders.HTTP_VERSION, version);111 return this;112 }113 /**114 * Sets the Http response status code.115 *116 * @param statusCode The status code header to respond with117 * @return The altered HttpMessage118 */119 public HttpMessage status(final HttpStatus statusCode) {120 statusCode(statusCode.value());121 reasonPhrase(statusCode.name());122 return this;123 }124 /**125 * Sets the Http response status code header.126 *127 * @param statusCode The status code header value to respond with128 * @return The altered HttpMessage129 */130 public HttpMessage statusCode(final Integer statusCode) {131 setHeader(HttpMessageHeaders.HTTP_STATUS_CODE, statusCode);132 return this;133 }134 /**135 * Sets the Http response reason phrase header.136 *137 * @param reasonPhrase The reason phrase header value to use138 * @return The altered HttpMessage139 */140 public HttpMessage reasonPhrase(final String reasonPhrase) {141 setHeader(HttpMessageHeaders.HTTP_REASON_PHRASE, reasonPhrase);142 return this;143 }144 /**145 * Sets the Http request request uri header.146 *147 * @param requestUri The request uri header value to use148 * @return The altered HttpMessage149 */150 public HttpMessage uri(final String requestUri) {151 setHeader(EndpointUriResolver.ENDPOINT_URI_HEADER_NAME, requestUri);152 setHeader(HttpMessageHeaders.HTTP_REQUEST_URI, requestUri);153 return this;154 }155 /**156 * Sets the Http request content type header.157 *158 * @param contentType The content type header value to use159 * @return The altered HttpMessage160 */161 public HttpMessage contentType(final String contentType) {162 setHeader("Content-Type", contentType);163 return this;164 }165 /**166 * Sets the Http accepted content type header for response.167 *168 * @param accept The accept header value to set169 * @return The altered HttpMessage170 */171 public HttpMessage accept(final String accept) {172 setHeader("Accept", accept);173 return this;174 }175 /**176 * Sets the Http request context path header.177 *178 * @param contextPath The context path header value to use179 * @return The altered HttpMessage180 */181 public HttpMessage contextPath(final String contextPath) {182 setHeader(HttpMessageHeaders.HTTP_CONTEXT_PATH, contextPath);183 return this;184 }185 /**186 * Sets the Http request query params query String. Query String is a compilation of key-value pairs separated187 * by comma character e.g. key1=value1[","key2=value2]. Query String can be empty.188 *189 * @param queryParamString The query parameter string to evaluate190 * @return The altered HttpMessage191 */192 public HttpMessage queryParams(final String queryParamString) {193 header(HttpMessageHeaders.HTTP_QUERY_PARAMS, queryParamString);194 header(EndpointUriResolver.QUERY_PARAM_HEADER_NAME, queryParamString);195 Stream.of(queryParamString.split(","))196 .map(keyValue -> Optional.ofNullable(StringUtils.split(keyValue, "=")).orElse(new String[]{keyValue, ""}))197 .filter(keyValue -> StringUtils.hasText(keyValue[0]))198 .forEach(keyValue -> this.addQueryParam(keyValue[0], keyValue[1]));199 return this;200 }201 /**202 * Sets a new Http request query param.203 *204 * @param name The name of the request query parameter205 * @return The altered HttpMessage206 */207 public HttpMessage queryParam(final String name) {208 return queryParam(name, null);209 }210 /**211 * Sets a new Http request query param.212 *213 * @param name The name of the request query parameter214 * @param value The value of the request query parameter215 * @return The altered HttpMessage216 */217 public HttpMessage queryParam(final String name, final String value) {218 if (!StringUtils.hasText(name)) {219 throw new CitrusRuntimeException("Invalid query param name - must not be empty!");220 }221 this.addQueryParam(name, value);222 final String queryParamString = queryParams.entrySet()223 .stream()224 .map(this::outputQueryParam)225 .collect(Collectors.joining(","));226 header(HttpMessageHeaders.HTTP_QUERY_PARAMS, queryParamString);227 header(EndpointUriResolver.QUERY_PARAM_HEADER_NAME, queryParamString);228 return this;229 }230 /**231 * Sets request path that is dynamically added to base uri.232 *233 * @param path The part of the path to add234 * @return The altered HttpMessage235 */236 public HttpMessage path(final String path) {237 header(HttpMessageHeaders.HTTP_REQUEST_URI, path);238 header(EndpointUriResolver.REQUEST_PATH_HEADER_NAME, path);239 return this;240 }241 /**242 * Sets new header name value pair.243 *244 * @param headerName The name of the header245 * @param headerValue The value of the header246 * @return The altered HttpMessage247 */248 public HttpMessage header(final String headerName, final Object headerValue) {249 return (HttpMessage) super.setHeader(headerName, headerValue);250 }251 @Override252 public HttpMessage setHeader(final String headerName, final Object headerValue) {253 return (HttpMessage) super.setHeader(headerName, headerValue);254 }255 @Override256 public HttpMessage addHeaderData(final String headerData) {257 return (HttpMessage) super.addHeaderData(headerData);258 }259 /**260 * Gets the Http request method.261 *262 * @return The used HttpMethod263 */264 public HttpMethod getRequestMethod() {265 final Object method = getHeader(HttpMessageHeaders.HTTP_REQUEST_METHOD);266 if (method != null) {267 return HttpMethod.valueOf(method.toString());268 }269 return null;270 }271 /**272 * Gets the Http request request uri.273 *274 * @return The request uri275 */276 public String getUri() {277 final Object requestUri = getHeader(HttpMessageHeaders.HTTP_REQUEST_URI);278 if (requestUri != null) {279 return requestUri.toString();280 }281 return null;282 }283 /**284 * Gets the Http request context path.285 *286 * @return the context path287 */288 public String getContextPath() {289 final Object contextPath = getHeader(HttpMessageHeaders.HTTP_CONTEXT_PATH);290 if (contextPath != null) {291 return contextPath.toString();292 }293 return null;294 }295 /**296 * Gets the Http content type header.297 *298 * @return the content type header value299 */300 public String getContentType() {301 final Object contentType = getHeader(HttpMessageHeaders.HTTP_CONTENT_TYPE);302 if (contentType != null) {303 return contentType.toString();304 }305 return null;306 }307 /**308 * Gets the accept header.309 *310 * @return The accept header value311 */312 public String getAccept() {313 final Object accept = getHeader("Accept");314 if (accept != null) {315 return accept.toString();316 }317 return null;318 }319 /**320 * Gets the Http request query params.321 *322 * @return The query parameters as a key value map323 */324 public Map<String, Collection<String>> getQueryParams() {325 return queryParams;326 }327 /**328 * Gets the Http request query param string.329 *330 * @return The query parameter as string331 */332 public String getQueryParamString() {333 return Optional.ofNullable(getHeader(HttpMessageHeaders.HTTP_QUERY_PARAMS)).map(Object::toString).orElse("");334 }335 /**336 * Gets the Http response status code.337 *338 * @return The status code of the message339 */340 public HttpStatus getStatusCode() {341 final Object statusCode = getHeader(HttpMessageHeaders.HTTP_STATUS_CODE);342 if (statusCode != null) {343 if (statusCode instanceof HttpStatus) {344 return (HttpStatus) statusCode;345 } else if (statusCode instanceof Integer) {346 return HttpStatus.valueOf((Integer) statusCode);347 } else {348 return HttpStatus.valueOf(Integer.valueOf(statusCode.toString()));349 }350 }351 return null;352 }353 /**354 * Gets the Http response reason phrase.355 *356 * @return The reason phrase of the message357 */358 public String getReasonPhrase() {359 final Object reasonPhrase = getHeader(HttpMessageHeaders.HTTP_REASON_PHRASE);360 if (reasonPhrase != null) {361 return reasonPhrase.toString();362 }363 return null;364 }365 /**366 * Gets the Http version.367 *368 * @return The http version of the message369 */370 public String getVersion() {371 final Object version = getHeader(HttpMessageHeaders.HTTP_VERSION);372 if (version != null) {373 return version.toString();374 }375 return null;376 }377 /**378 * Gets the request path after the context path.379 *380 * @return The request path of the message381 */382 public String getPath() {383 final Object path = getHeader(EndpointUriResolver.REQUEST_PATH_HEADER_NAME);384 if (path != null) {385 return path.toString();386 }387 return null;388 }389 /**390 * Gets the cookies.391 *392 * @return The list of cookies for this message393 */394 public List<Cookie> getCookies() {395 return new ArrayList<>(cookies.values());396 }397 /**398 * Get the cookies represented as a map with the cookie name as key399 *400 * @return A map of Cookies identified by the cookie name401 */402 private Map<String, Cookie> getCookiesMap() {403 return cookies;404 }405 /**406 * Sets the cookies.407 *408 * @param cookies The cookies to set409 */410 public void setCookies(final Cookie[] cookies) {411 this.cookies.clear();412 if (cookies != null) {413 for (final Cookie cookie : cookies) {414 cookie(cookie);415 }416 }...

Full Screen

Full Screen

getCookiesMap

Using AI Code Generation

copy

Full Screen

1Cookies cookies = new Cookies();2cookies.add("cookie1", "value1");3cookies.add("cookie2", "value2");4http()5 .client(client)6 .send()7 .post("/test")8 .contentType("application/json")9 .payload(jsonMessage)10 .cookies(cookies);11Cookies cookies = new Cookies();12cookies.add("cookie1", "value1");13cookies.add("cookie2", "value2");14http()15 .client(client)16 .send()17 .post("/test")18 .contentType("application/json")19 .payload(jsonMessage)20 .cookies(cookies);21Cookies cookies = new Cookies();22cookies.add("cookie1", "value1");23cookies.add("cookie2", "value2");24http()25 .client(client)26 .send()27 .post("/test")28 .contentType("application/json")29 .payload(jsonMessage)30 .cookies(cookies);31Cookies cookies = new Cookies();32cookies.add("cookie1", "value1");33cookies.add("cookie2", "value2");34http()35 .client(client)36 .send()37 .post("/test")38 .contentType("application/json")39 .payload(jsonMessage)40 .cookies(cookies);41Cookies cookies = new Cookies();42cookies.add("cookie1", "value1");43cookies.add("cookie2", "value2");44http()45 .client(client)46 .send()47 .post("/test")48 .contentType("application/json")49 .payload(jsonMessage)50 .cookies(cookies);51Cookies cookies = new Cookies();52cookies.add("cookie1", "value1");53cookies.add("cookie2", "value2");54http()55 .client(client)56 .send()57 .post("/test")58 .contentType("application/json")59 .payload(jsonMessage)60 .cookies(cookies);

Full Screen

Full Screen

getCookiesMap

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.http.message.HttpMessage;2import com.consol.citrus.http.message.HttpMessageHeaders;3import com.consol.citrus.testng.AbstractTestNGCitrusTest;4import java.util.Map;5import static com.consol.citrus.actions.EchoAction.Builder.echo;6import static com.consol.citrus.actions.SendMessageAction.Builder.send;7import static com.consol.citrus.http.actions.HttpActionBuilder.http;8public class Test extends AbstractTestNGCitrusTest {9 public void test() {10 variable("cookieName", "testCookie");11 variable("cookieValue", "testCookieValue");12 http().client("httpClient")13 .send()14 .get("/test")15 .cookie("${cookieName}", "${cookieValue}");16 http().client("httpClient")17 .receive()18 .response(HttpStatus.OK)19 .messageType(MessageType.PLAINTEXT)20 .extractFromPayload("(.*)", "responseMessage")21 .extractFromHeader(HttpMessageHeaders.HTTP_RESPONSE_COOKIES, "cookies");22 echo("${responseMessage}");23 echo("${cookies}");24 Map<String, String> cookiesMap = (Map<String, String>) context.getVariable("cookies");25 echo(cookiesMap.get("${cookieName}"));26 }27}28import com.consol.citrus.annotations.CitrusTest;29import com.consol.citrus.dsl.annotations.CitrusResource;30import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;31import com.consol.citrus.http.client.HttpClient;32import org.springframework.beans.factory.annotation.Autowired;33import org.springframework.http.HttpStatus;34import org

Full Screen

Full Screen

getCookiesMap

Using AI Code Generation

copy

Full Screen

1public void testCookies() {2 http().client("httpClient")3 .send()4 .get("/api/cookies")5 .receive()6 .response(HttpStatus.OK);7 http().client("httpClient")8 .send()9 .get("/api/cookies")10 .receive()11 .response(HttpStatus.OK)12 .validate("$['cookies'].size()", 1)13 .validate("$['cookies'][0]['name']", "myCookie")14 .validate("$['cookies'][0]['value']", "myValue");15}

Full Screen

Full Screen

getCookiesMap

Using AI Code Generation

copy

Full Screen

1public void testHttpCookies() {2 http()3 .client(httpClient)4 .send()5 .get("/cookie")6 .accept("application/json");7 http()8 .client(httpClient)9 .receive()10 .response(HttpStatus.OK)11 .extractFromPayload("$.cookie", "cookieValue")12 .validate((message, context) -> {13 Map<String, String> cookiesMap = message.getCookiesMap();14 Assert.assertTrue(cookiesMap.containsKey("cookie"));15 Assert.assertEquals(cookiesMap.get("cookie"), "value");16 });17 http()18 .client(httpClient)19 .send()20 .get("/cookie")21 .accept("application/json")22 .setCookies("cookie", "${cookieValue}");23 http()24 .client(httpClient)25 .receive()26 .response(HttpStatus.OK)27 .validate((message, context) -> {28 Map<String, String> cookiesMap = message.getCookiesMap();29 Assert.assertTrue(cookiesMap.containsKey("cookie"));30 Assert.assertEquals(cookiesMap.get("cookie"), "value");31 });32}33public void testHttpCookies() {34 http()35 .client(httpClient)36 .send()37 .get("/cookie")38 .accept("application/json");39 http()40 .client(httpClient)41 .receive()42 .response(HttpStatus.OK)43 .extractFromPayload("$.cookie", "cookieValue")44 .validate((message, context) -> {45 Map<String, String> cookiesMap = message.getCookiesMap();46 Assert.assertTrue(cookiesMap.containsKey("cookie"));47 Assert.assertEquals(cookiesMap.get("cookie"), "value");48 });49 http()

Full Screen

Full Screen

getCookiesMap

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.annotations.CitrusTest;2import com.consol.citrus.dsl.junit.JUnit4CitrusTestRunner;3import com.consol.citrus.http.actions.HttpActionBuilder;4import com.consol.citrus.http.client.HttpClient;5import com.consol.citrus.message.MessageType;6import org.springframework.beans.factory.annotation.Autowired;7import org.springframework.http.HttpStatus;8import org.testng.annotations.Test;9public class HttpTest extends JUnit4CitrusTestRunner {10 private HttpClient httpClient;11 public void httpTest() {12 variable("cookies", "");13 variable("JSESSIONID", "");14 http().client(httpClient)15 .send()16 .post("/login")17 .payload("<login>citrus</login>");18 http().client(httpClient)19 .receive()20 .response(HttpStatus.OK)21 .messageType(MessageType.PLAINTEXT)22 .extractFromPayload("(?s)(.*)(</login>)(.*)", 1)23 .header("Set-Cookie", "${cookies}");24 echo("${cookies}");25 httpAction(new HttpActionBuilder()26 .client(httpClient)27 .send()28 .get("/home")29 .header("Cookie", "${cookies}"));30 http().client(httpClient)31 .receive()32 .response(HttpStatus.OK)33 .messageType(MessageType.PLAINTEXT)34 .extractFromPayload("(?s)(.*)(</login>)(.*)", 1)35 .header("Set-Cookie", "${cookies}");36 echo("${cookies}");37 httpAction(new HttpActionBuilder()38 .client(httpClient)39 .send()40 .get("/home")41 .header("Cookie", "${cookies}"));42 http().client(httpClient)43 .receive()44 .response(HttpStatus.OK)45 .messageType(MessageType.PLAINTEXT)46 .extractFromPayload("(?s)(.*)(</login>)(.*)", 1)47 .header("Set

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful