Skip to content

Commit

Permalink
Adding methods to retrieve current HTTP method+cookies in Java API
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Rivera committed Jan 30, 2018
1 parent a20ac4b commit 88d52da
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
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

0 comments on commit 88d52da

Please sign in to comment.