Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding methods to retrieve current HTTP method+cookies in Java API #233

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,16 @@ public String getUrl() {
return this.url;
}

@Override
public String getMethod() {
return this.method;
}

@Override
public List<WSCookie> getCookies() {
return new ArrayList<>(cookies);
}

@Override
public Map<String, List<String>> getHeaders() {
return new HashMap<>(this.headers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package play.libs.ws.ahc
import java.time.Duration
import java.util.Collections

import akka.util.ByteString
import org.specs2.mock.Mockito
import org.specs2.mutable._
import play.libs.oauth.OAuth
Expand All @@ -15,6 +14,7 @@ import play.shaded.ahc.io.netty.handler.codec.http.HttpHeaders
import play.shaded.ahc.org.asynchttpclient.{ Request, RequestBuilderBase, SignatureCalculator }

import scala.collection.JavaConverters._
import scala.collection.mutable
import scala.compat.java8.OptionConverters._

class AhcWSRequestSpec extends Specification with Mockito with DefaultBodyReadables with DefaultBodyWritables {
Expand All @@ -24,6 +24,7 @@ class AhcWSRequestSpec extends Specification with Mockito with DefaultBodyReadab
"Have GET method as the default" in {
val client = mock[StandaloneAhcWSClient]
val request = new StandaloneAhcWSRequest(client, "http://example.com", /*materializer*/ null)
request.getMethod must be_==("GET")
request.buildRequest().getMethod must be_==("GET")
}

Expand All @@ -37,6 +38,14 @@ class AhcWSRequestSpec extends Specification with Mockito with DefaultBodyReadab

"For POST requests" in {

"get method" in {
val client = mock[StandaloneAhcWSClient]
val req = new StandaloneAhcWSRequest(client, "http://playframework.com/", null)
.setMethod("POST")

req.getMethod must be_==("POST")
}

"set text/plain content-types for text bodies" in {
val client = mock[StandaloneAhcWSClient]
val formEncoding = java.net.URLEncoder.encode("param1=value1", "UTF-8")
Expand Down Expand Up @@ -417,6 +426,18 @@ class AhcWSRequestSpec extends Specification with Mockito with DefaultBodyReadab
new WSCookieBuilder().setName(name).setValue(value).build()
}

"get existing cookies" in {
val client = mock[StandaloneAhcWSClient]
val request = new StandaloneAhcWSRequest(client, "http://example.com", /*materializer*/ null)
.addCookie(cookie("cookie1", "value1"))

val cookiesInRequest: mutable.Buffer[WSCookie] = request.getCookies.asScala
cookiesInRequest.size must beEqualTo(1)
val cookieInRequest: WSCookie = cookiesInRequest.head
cookieInRequest.getName must beEqualTo("cookie1")
cookieInRequest.getValue must beEqualTo("value1")
}

"add a new cookie" in {
val client = mock[StandaloneAhcWSClient]
val request = new StandaloneAhcWSRequest(client, "http://example.com", /*materializer*/ null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,20 @@ public interface StandaloneWSRequest {
*/
String getUrl();

/**
* @return the HTTP method of the request.
*/
default String getMethod() {
throw new UnsupportedOperationException();
}

/**
* @return the cookies (a copy to prevent side-effects).
*/
default List<WSCookie> getCookies() {
throw new UnsupportedOperationException();
}

/**
* @return the headers (a copy to prevent side-effects). This has not passed through an internal request builder and so will not be signed.
*/
Expand Down