diff --git a/play-ahc-ws-standalone/src/main/java/play/libs/ws/ahc/StandaloneAhcWSRequest.java b/play-ahc-ws-standalone/src/main/java/play/libs/ws/ahc/StandaloneAhcWSRequest.java index b57780fa..2fd8c6b3 100644 --- a/play-ahc-ws-standalone/src/main/java/play/libs/ws/ahc/StandaloneAhcWSRequest.java +++ b/play-ahc-ws-standalone/src/main/java/play/libs/ws/ahc/StandaloneAhcWSRequest.java @@ -270,6 +270,16 @@ public String getUrl() { return this.url; } + @Override + public String getMethod() { + return this.method; + } + + @Override + public List getCookies() { + return new ArrayList<>(cookies); + } + @Override public Map> getHeaders() { return new HashMap<>(this.headers); diff --git a/play-ahc-ws-standalone/src/test/scala/play/libs/ws/ahc/AhcWSRequestSpec.scala b/play-ahc-ws-standalone/src/test/scala/play/libs/ws/ahc/AhcWSRequestSpec.scala index 0531e501..4e5c9817 100644 --- a/play-ahc-ws-standalone/src/test/scala/play/libs/ws/ahc/AhcWSRequestSpec.scala +++ b/play-ahc-ws-standalone/src/test/scala/play/libs/ws/ahc/AhcWSRequestSpec.scala @@ -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 @@ -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 { @@ -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") } @@ -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") @@ -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) diff --git a/play-ws-standalone/src/main/java/play/libs/ws/StandaloneWSRequest.java b/play-ws-standalone/src/main/java/play/libs/ws/StandaloneWSRequest.java index fbdabc2e..36548faf 100644 --- a/play-ws-standalone/src/main/java/play/libs/ws/StandaloneWSRequest.java +++ b/play-ws-standalone/src/main/java/play/libs/ws/StandaloneWSRequest.java @@ -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 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. */