@Override public Cookie build(NottableString name, NottableString value) { return new Cookie(name, value); }
@Override public void serialize(Cookies collection, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeStartObject(); for (Cookie cookie : collection.getEntries()) { jgen.writeStringField(serialiseNottableString(cookie.getName()), serialiseNottableString(cookie.getValue())); } jgen.writeEndObject(); }
.withPath(calculatePath("some_pathRequest")) .withCookies( cookie(string("requestCookieNameOne"), not("requestCookieValueOne")), cookie("requestCookieNameTwo", "requestCookieValueTwo") .withPath(calculatePath("some_pathRequest")) .withCookies( cookie("requestCookieNameOne", "OTHERrequestCookieValueOne"), cookie("requestCookieNameTwo", "requestCookieValueTwo") ), headersToIgnore)
.withPath(calculatePath("some_pathRequest")) .withCookies( cookie(not("requestCookieNameOne"), string("requestCookieValueOne")), cookie("requestCookieNameTwo", "requestCookieValueTwo") .withPath(calculatePath("some_pathRequest")) .withCookies( cookie("OTHERrequestCookieNameOne", "requestCookieValueOne"), cookie("requestCookieNameTwo", "requestCookieValueTwo") ), headersToIgnore)
@Override public String serialize(int numberOfSpacesToIndent, Cookie cookie) { return NEW_LINE + Strings.padStart("", numberOfSpacesToIndent * INDENT_SIZE, ' ') + "new Cookie(" + NottableStringToJavaSerializer.serializeNottableString(cookie.getName()) + ", " + NottableStringToJavaSerializer.serializeNottableString(cookie.getValue()) + ")"; }
.withPath(calculatePath("some_pathRequest")) .withCookies( cookie(not("requestCookieNameOne"), not("requestCookieValueOne")), cookie("requestCookieNameTwo", "requestCookieValueTwo") .withPath(calculatePath("some_pathRequest")) .withCookies( cookie("requestCookieNameOne", "requestCookieValueOne"), cookie("requestCookieNameTwo", "requestCookieValueTwo") ), headersToIgnore) .withPath(calculatePath("some_pathRequest")) .withCookies( cookie("requestCookieNameOne", "requestCookieValueOne"), cookie("requestCookieNameTwo", "requestCookieValueTwo") ), headersToIgnore)
public static Cookie cookie(String name, String value) { return new Cookie(name, value); }
private String getCookieHeader(HttpRequest request) { List<Cookie> cookies = new ArrayList<Cookie>(); for (org.mockserver.model.Cookie cookie : request.getCookieList()) { cookies.add(new DefaultCookie(cookie.getName().getValue(), cookie.getValue().getValue())); } if (cookies.size() > 0) { return " -H '" + COOKIE + ": " + ClientCookieEncoder.LAX.encode(cookies) + "'"; } else { return ""; } } }
.withPath(calculatePath("some_pathRequest")) .withCookies( cookie(not("requestCookieNameOne"), not("requestCookieValueOne")), cookie("requestCookieNameTwo", "requestCookieValueTwo") .withPath(calculatePath("some_pathRequest")) .withCookies( cookie("OTHERrequestCookieNameOne", "requestCookieValueOne"), cookie("requestCookieNameTwo", "requestCookieValueTwo") ), headersToIgnore) .withPath(calculatePath("some_pathRequest")) .withCookies( cookie("requestCookieNameOne", "OTHERrequestCookieValueOne"), cookie("requestCookieNameTwo", "requestCookieValueTwo") ), headersToIgnore)
public static Cookie cookie(NottableString name, NottableString value) { return new Cookie(name, value); } }
private void setCookies(HttpRequest httpRequest, FullHttpRequest request) { List<io.netty.handler.codec.http.cookie.Cookie> cookies = new ArrayList<io.netty.handler.codec.http.cookie.Cookie>(); for (org.mockserver.model.Cookie cookie : httpRequest.getCookieList()) { cookies.add(new io.netty.handler.codec.http.cookie.DefaultCookie(cookie.getName().getValue(), cookie.getValue().getValue())); } if (cookies.size() > 0) { request.headers().set( COOKIE.toString(), io.netty.handler.codec.http.cookie.ClientCookieEncoder.LAX.encode(cookies) ); } }
.withPath(calculatePath("some_pathRequest")) .withCookies( cookie("requestCookieNameOne", "requestCookieValueOne"), cookie("requestCookieNameTwo", "requestCookieValueTwo") .withBody("some_body_response") .withCookies( cookie("responseCookieNameOne", "responseCookieValueOne"), cookie("responseCookieNameTwo", "responseCookieValueTwo") .withBody("some_body_response") .withCookies( cookie("responseCookieNameOne", "responseCookieValueOne"), cookie("responseCookieNameTwo", "responseCookieValueTwo") cookie("requestCookieNameOne", "requestCookieValueOne"), cookie("requestCookieNameTwo", "requestCookieValueTwo") ), headersToIgnore) .withBody("some_body_response") .withCookies( cookie("responseCookieNameOne", "responseCookieValueOne"), cookie("responseCookieNameTwo", "responseCookieValueTwo") .withBody("some_body_response") .withCookies( cookie("responseCookieNameOne", "responseCookieValueOne"), cookie("responseCookieNameTwo", "responseCookieValueTwo")
private void setCookies(HttpRequest httpRequest, HttpServletRequest httpServletRequest) { Cookies cookies = new Cookies(); if (httpServletRequest.getCookies() != null) { for (javax.servlet.http.Cookie cookie : httpServletRequest.getCookies()) { cookies.withEntry(new Cookie(cookie.getName(), cookie.getValue())); } } httpRequest.withCookies(cookies); }
private void setCookies(HttpResponse response, DefaultFullHttpResponse fullHttpResponse) { if (response.getCookieList() != null) { List<Cookie> cookieValues = new ArrayList<Cookie>(); for (org.mockserver.model.Cookie cookie : response.getCookieList()) { if (!cookieHeaderAlreadyExists(response, cookie)) { cookieValues.add(new DefaultCookie(cookie.getName().getValue(), cookie.getValue().getValue())); } } for (Cookie cookieValue : cookieValues) { fullHttpResponse.headers().add(SET_COOKIE, ServerCookieEncoder.LAX.encode(cookieValue)); } } }
.withReasonPhrase(HttpStatusCode.ACCEPTED_202.reasonPhrase()) .withBody("some_body_response") .withCookies(cookie("cookieNameResponse", "cookieValueResponse")) ); .withReasonPhrase(HttpStatusCode.ACCEPTED_202.reasonPhrase()) .withBody("some_body_response") .withCookies(cookie("cookieNameResponse", "cookieValueResponse")) .withHeaders( header(SET_COOKIE.toString(), "cookieNameResponse=cookieValueResponse") header("requestHeaderNameTwo", "requestHeaderValueTwo") .withCookies(cookie("cookieNameRequest", "cookieValueRequest")), headersToIgnore) ); .withReasonPhrase(HttpStatusCode.ACCEPTED_202.reasonPhrase()) .withBody("some_body_response") .withCookies(cookie("cookieNameResponse", "cookieValueResponse")) .withHeaders( header(SET_COOKIE.toString(), "cookieNameResponse=cookieValueResponse") header("requestHeaderNameTwo", "requestHeaderValueTwo") .withCookies(cookie("cookieNameRequest", "cookieValueRequest")), headersToIgnore) );
public Cookie buildObject() { return new Cookie(getName(), getValue()); } }
private boolean cookieHeaderAlreadyExists(HttpResponse response, org.mockserver.model.Cookie cookieValue) { List<String> setCookieHeaders = response.getHeader(SET_COOKIE.toString()); for (String setCookieHeader : setCookieHeaders) { String existingCookieName = ClientCookieDecoder.LAX.decode(setCookieHeader).name(); String existingCookieValue = ClientCookieDecoder.LAX.decode(setCookieHeader).value(); if (existingCookieName.equalsIgnoreCase(cookieValue.getName().getValue()) && existingCookieValue.equalsIgnoreCase(cookieValue.getValue().getValue())) { return true; } } return false; } }
.withReasonPhrase(HttpStatusCode.ACCEPTED_202.reasonPhrase()) .withBody("some_body_response") .withCookies(cookie("cookieNameResponse", "cookieValueResponse")) ); .withReasonPhrase(HttpStatusCode.ACCEPTED_202.reasonPhrase()) .withBody("some_body_response") .withCookies(cookie("cookieNameResponse", "cookieValueResponse")) .withHeaders( header(SET_COOKIE.toString(), "cookieNameResponse=cookieValueResponse") .withCookies(cookie("cookieNameRequest", "cookieValueRequest")), headersToIgnore) ); .withReasonPhrase(HttpStatusCode.ACCEPTED_202.reasonPhrase()) .withBody("some_body_response") .withCookies(cookie("cookieNameResponse", "cookieValueResponse")) .withHeaders( header(SET_COOKIE.toString(), "cookieNameResponse=cookieValueResponse") .withCookies(cookie("cookieNameRequest", "cookieValueRequest")), headersToIgnore) );
private void setCookies(HttpResponse httpResponse) { Cookies cookies = new Cookies(); for (Header header : httpResponse.getHeaderList()) { if (header.getName().getValue().equalsIgnoreCase("Set-Cookie")) { for (NottableString cookieHeader : header.getValues()) { io.netty.handler.codec.http.cookie.Cookie httpCookie = ClientCookieDecoder.LAX.decode(cookieHeader.getValue()); String name = httpCookie.name().trim(); String value = httpCookie.value().trim(); cookies.withEntry(new Cookie(name, value)); } } if (header.getName().getValue().equalsIgnoreCase("Cookie")) { for (NottableString cookieHeader : header.getValues()) { for (io.netty.handler.codec.http.cookie.Cookie httpCookie : ServerCookieDecoder.LAX.decode(cookieHeader.getValue())) { String name = httpCookie.name().trim(); String value = httpCookie.value().trim(); cookies.withEntry(new Cookie(name, value)); } } } } if (!cookies.isEmpty()) { httpResponse.withCookies(cookies); } }
private boolean cookieHeaderAlreadyExists(HttpResponse response, Cookie cookieValue) { List<String> setCookieHeaders = response.getHeader(SET_COOKIE.toString()); for (String setCookieHeader : setCookieHeaders) { String existingCookieName = ClientCookieDecoder.LAX.decode(setCookieHeader).name(); String existingCookieValue = ClientCookieDecoder.LAX.decode(setCookieHeader).value(); if (existingCookieName.equalsIgnoreCase(cookieValue.getName().getValue()) && existingCookieValue.equalsIgnoreCase(cookieValue.getValue().getValue())) { return true; } } return false; }