1
0
mirror of synced 2026-05-22 18:53:15 +00:00

feat: request API with shared RequestOptions and FormData (#718)

This commit is contained in:
Yury Semikhatsky
2021-11-19 17:35:59 -08:00
committed by GitHub
parent 0076b8f8a9
commit 7d1026ea9c
10 changed files with 520 additions and 859 deletions
@@ -16,6 +16,7 @@
package com.microsoft.playwright;
import com.microsoft.playwright.options.*;
import java.nio.file.Path;
import java.util.*;
@@ -26,752 +27,6 @@ import java.util.*;
* logged in and vice versa.
*/
public interface APIRequestContext {
class DeleteOptions {
/**
* Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
public Object data;
/**
* Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.
*/
public Boolean failOnStatusCode;
/**
* Provides an object that will be serialized as html form using {@code application/x-www-form-urlencoded} encoding and sent as
* this request body. If this parameter is specified {@code content-type} header will be set to
* {@code application/x-www-form-urlencoded} unless explicitly provided.
*/
public Map<String, Object> form;
/**
* Allows to set HTTP headers.
*/
public Map<String, String> headers;
/**
* Whether to ignore HTTPS errors when sending network requests. Defaults to {@code false}.
*/
public Boolean ignoreHTTPSErrors;
/**
* Provides an object that will be serialized as html form using {@code multipart/form-data} encoding and sent as this request
* body. If this parameter is specified {@code content-type} header will be set to {@code multipart/form-data} unless explicitly
* provided. File values can be passed either as [File] or as file-like object [FilePayload] containing file name,
* mime-type and its content.
*/
public Map<String, Object> multipart;
/**
* Query parameters to be sent with the URL.
*/
public Map<String, Object> params;
/**
* Request timeout in milliseconds. Defaults to {@code 30000} (30 seconds). Pass {@code 0} to disable timeout.
*/
public Double timeout;
/**
* Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
public DeleteOptions setData(String data) {
this.data = data;
return this;
}
/**
* Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
public DeleteOptions setData(byte[] data) {
this.data = data;
return this;
}
/**
* Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
public DeleteOptions setData(Object data) {
this.data = data;
return this;
}
/**
* Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.
*/
public DeleteOptions setFailOnStatusCode(boolean failOnStatusCode) {
this.failOnStatusCode = failOnStatusCode;
return this;
}
/**
* Provides an object that will be serialized as html form using {@code application/x-www-form-urlencoded} encoding and sent as
* this request body. If this parameter is specified {@code content-type} header will be set to
* {@code application/x-www-form-urlencoded} unless explicitly provided.
*/
public DeleteOptions setForm(Map<String, Object> form) {
this.form = form;
return this;
}
/**
* Allows to set HTTP headers.
*/
public DeleteOptions setHeaders(Map<String, String> headers) {
this.headers = headers;
return this;
}
/**
* Whether to ignore HTTPS errors when sending network requests. Defaults to {@code false}.
*/
public DeleteOptions setIgnoreHTTPSErrors(boolean ignoreHTTPSErrors) {
this.ignoreHTTPSErrors = ignoreHTTPSErrors;
return this;
}
/**
* Provides an object that will be serialized as html form using {@code multipart/form-data} encoding and sent as this request
* body. If this parameter is specified {@code content-type} header will be set to {@code multipart/form-data} unless explicitly
* provided. File values can be passed either as [File] or as file-like object [FilePayload] containing file name,
* mime-type and its content.
*/
public DeleteOptions setMultipart(Map<String, Object> multipart) {
this.multipart = multipart;
return this;
}
/**
* Query parameters to be sent with the URL.
*/
public DeleteOptions setParams(Map<String, Object> params) {
this.params = params;
return this;
}
/**
* Request timeout in milliseconds. Defaults to {@code 30000} (30 seconds). Pass {@code 0} to disable timeout.
*/
public DeleteOptions setTimeout(double timeout) {
this.timeout = timeout;
return this;
}
}
class FetchOptions {
/**
* Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
public Object data;
/**
* Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.
*/
public Boolean failOnStatusCode;
/**
* Provides an object that will be serialized as html form using {@code application/x-www-form-urlencoded} encoding and sent as
* this request body. If this parameter is specified {@code content-type} header will be set to
* {@code application/x-www-form-urlencoded} unless explicitly provided.
*/
public Map<String, Object> form;
/**
* Allows to set HTTP headers.
*/
public Map<String, String> headers;
/**
* Whether to ignore HTTPS errors when sending network requests. Defaults to {@code false}.
*/
public Boolean ignoreHTTPSErrors;
/**
* If set changes the fetch method (e.g. <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT">PUT</a> or
* <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST">POST</a>). If not specified, GET method is
* used.
*/
public String method;
/**
* Provides an object that will be serialized as html form using {@code multipart/form-data} encoding and sent as this request
* body. If this parameter is specified {@code content-type} header will be set to {@code multipart/form-data} unless explicitly
* provided. File values can be passed either as [File] or as file-like object [FilePayload] containing file name,
* mime-type and its content.
*/
public Map<String, Object> multipart;
/**
* Query parameters to be sent with the URL.
*/
public Map<String, Object> params;
/**
* Request timeout in milliseconds. Defaults to {@code 30000} (30 seconds). Pass {@code 0} to disable timeout.
*/
public Double timeout;
/**
* Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
public FetchOptions setData(String data) {
this.data = data;
return this;
}
/**
* Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
public FetchOptions setData(byte[] data) {
this.data = data;
return this;
}
/**
* Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
public FetchOptions setData(Object data) {
this.data = data;
return this;
}
/**
* Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.
*/
public FetchOptions setFailOnStatusCode(boolean failOnStatusCode) {
this.failOnStatusCode = failOnStatusCode;
return this;
}
/**
* Provides an object that will be serialized as html form using {@code application/x-www-form-urlencoded} encoding and sent as
* this request body. If this parameter is specified {@code content-type} header will be set to
* {@code application/x-www-form-urlencoded} unless explicitly provided.
*/
public FetchOptions setForm(Map<String, Object> form) {
this.form = form;
return this;
}
/**
* Allows to set HTTP headers.
*/
public FetchOptions setHeaders(Map<String, String> headers) {
this.headers = headers;
return this;
}
/**
* Whether to ignore HTTPS errors when sending network requests. Defaults to {@code false}.
*/
public FetchOptions setIgnoreHTTPSErrors(boolean ignoreHTTPSErrors) {
this.ignoreHTTPSErrors = ignoreHTTPSErrors;
return this;
}
/**
* If set changes the fetch method (e.g. <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT">PUT</a> or
* <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST">POST</a>). If not specified, GET method is
* used.
*/
public FetchOptions setMethod(String method) {
this.method = method;
return this;
}
/**
* Provides an object that will be serialized as html form using {@code multipart/form-data} encoding and sent as this request
* body. If this parameter is specified {@code content-type} header will be set to {@code multipart/form-data} unless explicitly
* provided. File values can be passed either as [File] or as file-like object [FilePayload] containing file name,
* mime-type and its content.
*/
public FetchOptions setMultipart(Map<String, Object> multipart) {
this.multipart = multipart;
return this;
}
/**
* Query parameters to be sent with the URL.
*/
public FetchOptions setParams(Map<String, Object> params) {
this.params = params;
return this;
}
/**
* Request timeout in milliseconds. Defaults to {@code 30000} (30 seconds). Pass {@code 0} to disable timeout.
*/
public FetchOptions setTimeout(double timeout) {
this.timeout = timeout;
return this;
}
}
class GetOptions {
/**
* Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.
*/
public Boolean failOnStatusCode;
/**
* Allows to set HTTP headers.
*/
public Map<String, String> headers;
/**
* Whether to ignore HTTPS errors when sending network requests. Defaults to {@code false}.
*/
public Boolean ignoreHTTPSErrors;
/**
* Query parameters to be sent with the URL.
*/
public Map<String, Object> params;
/**
* Request timeout in milliseconds. Defaults to {@code 30000} (30 seconds). Pass {@code 0} to disable timeout.
*/
public Double timeout;
/**
* Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.
*/
public GetOptions setFailOnStatusCode(boolean failOnStatusCode) {
this.failOnStatusCode = failOnStatusCode;
return this;
}
/**
* Allows to set HTTP headers.
*/
public GetOptions setHeaders(Map<String, String> headers) {
this.headers = headers;
return this;
}
/**
* Whether to ignore HTTPS errors when sending network requests. Defaults to {@code false}.
*/
public GetOptions setIgnoreHTTPSErrors(boolean ignoreHTTPSErrors) {
this.ignoreHTTPSErrors = ignoreHTTPSErrors;
return this;
}
/**
* Query parameters to be sent with the URL.
*/
public GetOptions setParams(Map<String, Object> params) {
this.params = params;
return this;
}
/**
* Request timeout in milliseconds. Defaults to {@code 30000} (30 seconds). Pass {@code 0} to disable timeout.
*/
public GetOptions setTimeout(double timeout) {
this.timeout = timeout;
return this;
}
}
class HeadOptions {
/**
* Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.
*/
public Boolean failOnStatusCode;
/**
* Allows to set HTTP headers.
*/
public Map<String, String> headers;
/**
* Whether to ignore HTTPS errors when sending network requests. Defaults to {@code false}.
*/
public Boolean ignoreHTTPSErrors;
/**
* Query parameters to be sent with the URL.
*/
public Map<String, Object> params;
/**
* Request timeout in milliseconds. Defaults to {@code 30000} (30 seconds). Pass {@code 0} to disable timeout.
*/
public Double timeout;
/**
* Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.
*/
public HeadOptions setFailOnStatusCode(boolean failOnStatusCode) {
this.failOnStatusCode = failOnStatusCode;
return this;
}
/**
* Allows to set HTTP headers.
*/
public HeadOptions setHeaders(Map<String, String> headers) {
this.headers = headers;
return this;
}
/**
* Whether to ignore HTTPS errors when sending network requests. Defaults to {@code false}.
*/
public HeadOptions setIgnoreHTTPSErrors(boolean ignoreHTTPSErrors) {
this.ignoreHTTPSErrors = ignoreHTTPSErrors;
return this;
}
/**
* Query parameters to be sent with the URL.
*/
public HeadOptions setParams(Map<String, Object> params) {
this.params = params;
return this;
}
/**
* Request timeout in milliseconds. Defaults to {@code 30000} (30 seconds). Pass {@code 0} to disable timeout.
*/
public HeadOptions setTimeout(double timeout) {
this.timeout = timeout;
return this;
}
}
class PatchOptions {
/**
* Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
public Object data;
/**
* Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.
*/
public Boolean failOnStatusCode;
/**
* Provides an object that will be serialized as html form using {@code application/x-www-form-urlencoded} encoding and sent as
* this request body. If this parameter is specified {@code content-type} header will be set to
* {@code application/x-www-form-urlencoded} unless explicitly provided.
*/
public Map<String, Object> form;
/**
* Allows to set HTTP headers.
*/
public Map<String, String> headers;
/**
* Whether to ignore HTTPS errors when sending network requests. Defaults to {@code false}.
*/
public Boolean ignoreHTTPSErrors;
/**
* Provides an object that will be serialized as html form using {@code multipart/form-data} encoding and sent as this request
* body. If this parameter is specified {@code content-type} header will be set to {@code multipart/form-data} unless explicitly
* provided. File values can be passed either as [File] or as file-like object [FilePayload] containing file name,
* mime-type and its content.
*/
public Map<String, Object> multipart;
/**
* Query parameters to be sent with the URL.
*/
public Map<String, Object> params;
/**
* Request timeout in milliseconds. Defaults to {@code 30000} (30 seconds). Pass {@code 0} to disable timeout.
*/
public Double timeout;
/**
* Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
public PatchOptions setData(String data) {
this.data = data;
return this;
}
/**
* Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
public PatchOptions setData(byte[] data) {
this.data = data;
return this;
}
/**
* Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
public PatchOptions setData(Object data) {
this.data = data;
return this;
}
/**
* Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.
*/
public PatchOptions setFailOnStatusCode(boolean failOnStatusCode) {
this.failOnStatusCode = failOnStatusCode;
return this;
}
/**
* Provides an object that will be serialized as html form using {@code application/x-www-form-urlencoded} encoding and sent as
* this request body. If this parameter is specified {@code content-type} header will be set to
* {@code application/x-www-form-urlencoded} unless explicitly provided.
*/
public PatchOptions setForm(Map<String, Object> form) {
this.form = form;
return this;
}
/**
* Allows to set HTTP headers.
*/
public PatchOptions setHeaders(Map<String, String> headers) {
this.headers = headers;
return this;
}
/**
* Whether to ignore HTTPS errors when sending network requests. Defaults to {@code false}.
*/
public PatchOptions setIgnoreHTTPSErrors(boolean ignoreHTTPSErrors) {
this.ignoreHTTPSErrors = ignoreHTTPSErrors;
return this;
}
/**
* Provides an object that will be serialized as html form using {@code multipart/form-data} encoding and sent as this request
* body. If this parameter is specified {@code content-type} header will be set to {@code multipart/form-data} unless explicitly
* provided. File values can be passed either as [File] or as file-like object [FilePayload] containing file name,
* mime-type and its content.
*/
public PatchOptions setMultipart(Map<String, Object> multipart) {
this.multipart = multipart;
return this;
}
/**
* Query parameters to be sent with the URL.
*/
public PatchOptions setParams(Map<String, Object> params) {
this.params = params;
return this;
}
/**
* Request timeout in milliseconds. Defaults to {@code 30000} (30 seconds). Pass {@code 0} to disable timeout.
*/
public PatchOptions setTimeout(double timeout) {
this.timeout = timeout;
return this;
}
}
class PostOptions {
/**
* Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
public Object data;
/**
* Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.
*/
public Boolean failOnStatusCode;
/**
* Provides an object that will be serialized as html form using {@code application/x-www-form-urlencoded} encoding and sent as
* this request body. If this parameter is specified {@code content-type} header will be set to
* {@code application/x-www-form-urlencoded} unless explicitly provided.
*/
public Map<String, Object> form;
/**
* Allows to set HTTP headers.
*/
public Map<String, String> headers;
/**
* Whether to ignore HTTPS errors when sending network requests. Defaults to {@code false}.
*/
public Boolean ignoreHTTPSErrors;
/**
* Provides an object that will be serialized as html form using {@code multipart/form-data} encoding and sent as this request
* body. If this parameter is specified {@code content-type} header will be set to {@code multipart/form-data} unless explicitly
* provided. File values can be passed either as [File] or as file-like object [FilePayload] containing file name,
* mime-type and its content.
*/
public Map<String, Object> multipart;
/**
* Query parameters to be sent with the URL.
*/
public Map<String, Object> params;
/**
* Request timeout in milliseconds. Defaults to {@code 30000} (30 seconds). Pass {@code 0} to disable timeout.
*/
public Double timeout;
/**
* Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
public PostOptions setData(String data) {
this.data = data;
return this;
}
/**
* Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
public PostOptions setData(byte[] data) {
this.data = data;
return this;
}
/**
* Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
public PostOptions setData(Object data) {
this.data = data;
return this;
}
/**
* Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.
*/
public PostOptions setFailOnStatusCode(boolean failOnStatusCode) {
this.failOnStatusCode = failOnStatusCode;
return this;
}
/**
* Provides an object that will be serialized as html form using {@code application/x-www-form-urlencoded} encoding and sent as
* this request body. If this parameter is specified {@code content-type} header will be set to
* {@code application/x-www-form-urlencoded} unless explicitly provided.
*/
public PostOptions setForm(Map<String, Object> form) {
this.form = form;
return this;
}
/**
* Allows to set HTTP headers.
*/
public PostOptions setHeaders(Map<String, String> headers) {
this.headers = headers;
return this;
}
/**
* Whether to ignore HTTPS errors when sending network requests. Defaults to {@code false}.
*/
public PostOptions setIgnoreHTTPSErrors(boolean ignoreHTTPSErrors) {
this.ignoreHTTPSErrors = ignoreHTTPSErrors;
return this;
}
/**
* Provides an object that will be serialized as html form using {@code multipart/form-data} encoding and sent as this request
* body. If this parameter is specified {@code content-type} header will be set to {@code multipart/form-data} unless explicitly
* provided. File values can be passed either as [File] or as file-like object [FilePayload] containing file name,
* mime-type and its content.
*/
public PostOptions setMultipart(Map<String, Object> multipart) {
this.multipart = multipart;
return this;
}
/**
* Query parameters to be sent with the URL.
*/
public PostOptions setParams(Map<String, Object> params) {
this.params = params;
return this;
}
/**
* Request timeout in milliseconds. Defaults to {@code 30000} (30 seconds). Pass {@code 0} to disable timeout.
*/
public PostOptions setTimeout(double timeout) {
this.timeout = timeout;
return this;
}
}
class PutOptions {
/**
* Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
public Object data;
/**
* Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.
*/
public Boolean failOnStatusCode;
/**
* Provides an object that will be serialized as html form using {@code application/x-www-form-urlencoded} encoding and sent as
* this request body. If this parameter is specified {@code content-type} header will be set to
* {@code application/x-www-form-urlencoded} unless explicitly provided.
*/
public Map<String, Object> form;
/**
* Allows to set HTTP headers.
*/
public Map<String, String> headers;
/**
* Whether to ignore HTTPS errors when sending network requests. Defaults to {@code false}.
*/
public Boolean ignoreHTTPSErrors;
/**
* Provides an object that will be serialized as html form using {@code multipart/form-data} encoding and sent as this request
* body. If this parameter is specified {@code content-type} header will be set to {@code multipart/form-data} unless explicitly
* provided. File values can be passed either as [File] or as file-like object [FilePayload] containing file name,
* mime-type and its content.
*/
public Map<String, Object> multipart;
/**
* Query parameters to be sent with the URL.
*/
public Map<String, Object> params;
/**
* Request timeout in milliseconds. Defaults to {@code 30000} (30 seconds). Pass {@code 0} to disable timeout.
*/
public Double timeout;
/**
* Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
public PutOptions setData(String data) {
this.data = data;
return this;
}
/**
* Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
public PutOptions setData(byte[] data) {
this.data = data;
return this;
}
/**
* Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
public PutOptions setData(Object data) {
this.data = data;
return this;
}
/**
* Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.
*/
public PutOptions setFailOnStatusCode(boolean failOnStatusCode) {
this.failOnStatusCode = failOnStatusCode;
return this;
}
/**
* Provides an object that will be serialized as html form using {@code application/x-www-form-urlencoded} encoding and sent as
* this request body. If this parameter is specified {@code content-type} header will be set to
* {@code application/x-www-form-urlencoded} unless explicitly provided.
*/
public PutOptions setForm(Map<String, Object> form) {
this.form = form;
return this;
}
/**
* Allows to set HTTP headers.
*/
public PutOptions setHeaders(Map<String, String> headers) {
this.headers = headers;
return this;
}
/**
* Whether to ignore HTTPS errors when sending network requests. Defaults to {@code false}.
*/
public PutOptions setIgnoreHTTPSErrors(boolean ignoreHTTPSErrors) {
this.ignoreHTTPSErrors = ignoreHTTPSErrors;
return this;
}
/**
* Provides an object that will be serialized as html form using {@code multipart/form-data} encoding and sent as this request
* body. If this parameter is specified {@code content-type} header will be set to {@code multipart/form-data} unless explicitly
* provided. File values can be passed either as [File] or as file-like object [FilePayload] containing file name,
* mime-type and its content.
*/
public PutOptions setMultipart(Map<String, Object> multipart) {
this.multipart = multipart;
return this;
}
/**
* Query parameters to be sent with the URL.
*/
public PutOptions setParams(Map<String, Object> params) {
this.params = params;
return this;
}
/**
* Request timeout in milliseconds. Defaults to {@code 30000} (30 seconds). Pass {@code 0} to disable timeout.
*/
public PutOptions setTimeout(double timeout) {
this.timeout = timeout;
return this;
}
}
class StorageStateOptions {
/**
* The file path to save the storage state to. If {@code path} is a relative path, then it is resolved relative to current
@@ -804,8 +59,9 @@ public interface APIRequestContext {
* The method will automatically follow redirects.
*
* @param url Target URL.
* @param params Optional request parameters.
*/
APIResponse delete(String url, DeleteOptions options);
APIResponse delete(String url, RequestOptions params);
/**
* All responses returned by {@link APIRequestContext#get APIRequestContext.get()} and similar methods are stored in the
* memory, so that you can later call {@link APIResponse#body APIResponse.body()}. This method discards all stored
@@ -826,8 +82,9 @@ public interface APIRequestContext {
* context cookies from the response. The method will automatically follow redirects.
*
* @param urlOrRequest Target URL or Request to get all parameters from.
* @param params Optional request parameters.
*/
APIResponse fetch(String urlOrRequest, FetchOptions options);
APIResponse fetch(String urlOrRequest, RequestOptions params);
/**
* Sends HTTP(S) request and returns its response. The method will populate request cookies from the context and update
* context cookies from the response. The method will automatically follow redirects.
@@ -842,8 +99,9 @@ public interface APIRequestContext {
* context cookies from the response. The method will automatically follow redirects.
*
* @param urlOrRequest Target URL or Request to get all parameters from.
* @param params Optional request parameters.
*/
APIResponse fetch(Request urlOrRequest, FetchOptions options);
APIResponse fetch(Request urlOrRequest, RequestOptions params);
/**
* Sends HTTP(S) <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET">GET</a> request and returns its
* response. The method will populate request cookies from the context and update context cookies from the response. The
@@ -860,8 +118,9 @@ public interface APIRequestContext {
* method will automatically follow redirects.
*
* @param url Target URL.
* @param params Optional request parameters.
*/
APIResponse get(String url, GetOptions options);
APIResponse get(String url, RequestOptions params);
/**
* Sends HTTP(S) <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD">HEAD</a> request and returns its
* response. The method will populate request cookies from the context and update context cookies from the response. The
@@ -878,8 +137,9 @@ public interface APIRequestContext {
* method will automatically follow redirects.
*
* @param url Target URL.
* @param params Optional request parameters.
*/
APIResponse head(String url, HeadOptions options);
APIResponse head(String url, RequestOptions params);
/**
* Sends HTTP(S) <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH">PATCH</a> request and returns
* its response. The method will populate request cookies from the context and update context cookies from the response.
@@ -896,8 +156,9 @@ public interface APIRequestContext {
* The method will automatically follow redirects.
*
* @param url Target URL.
* @param params Optional request parameters.
*/
APIResponse patch(String url, PatchOptions options);
APIResponse patch(String url, RequestOptions params);
/**
* Sends HTTP(S) <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST">POST</a> request and returns its
* response. The method will populate request cookies from the context and update context cookies from the response. The
@@ -914,8 +175,9 @@ public interface APIRequestContext {
* method will automatically follow redirects.
*
* @param url Target URL.
* @param params Optional request parameters.
*/
APIResponse post(String url, PostOptions options);
APIResponse post(String url, RequestOptions params);
/**
* Sends HTTP(S) <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT">PUT</a> request and returns its
* response. The method will populate request cookies from the context and update context cookies from the response. The
@@ -932,8 +194,9 @@ public interface APIRequestContext {
* method will automatically follow redirects.
*
* @param url Target URL.
* @param params Optional request parameters.
*/
APIResponse put(String url, PutOptions options);
APIResponse put(String url, RequestOptions params);
/**
* Returns storage state for this request context, contains current cookies and local storage snapshot if it was passed to
* the constructor.
@@ -8,6 +8,7 @@ import com.microsoft.playwright.APIResponse;
import com.microsoft.playwright.PlaywrightException;
import com.microsoft.playwright.Request;
import com.microsoft.playwright.options.FilePayload;
import com.microsoft.playwright.options.RequestOptions;
import java.io.File;
import java.nio.charset.StandardCharsets;
@@ -17,7 +18,6 @@ import java.util.LinkedHashMap;
import java.util.Map;
import static com.microsoft.playwright.impl.Serialization.*;
import static com.microsoft.playwright.impl.Utils.convertType;
import static com.microsoft.playwright.impl.Utils.toFilePayload;
class APIRequestContextImpl extends ChannelOwner implements APIRequestContext {
@@ -26,10 +26,8 @@ class APIRequestContextImpl extends ChannelOwner implements APIRequestContext {
}
@Override
public APIResponse delete(String url, DeleteOptions options) {
FetchOptions fetchOptions = toFetchOptions(options);
fetchOptions.method = "DELETE";
return fetch(url, fetchOptions);
public APIResponse delete(String url, RequestOptions options) {
return fetch(url, ensureOptions(options, "DELETE"));
}
@Override
@@ -38,14 +36,15 @@ class APIRequestContextImpl extends ChannelOwner implements APIRequestContext {
}
@Override
public APIResponse fetch(String urlOrRequest, FetchOptions options) {
return withLogging("APIRequestContext.fetch", () -> fetchImpl(urlOrRequest, options));
public APIResponse fetch(String urlOrRequest, RequestOptions options) {
return withLogging("APIRequestContext.fetch", () -> fetchImpl(urlOrRequest, (RequestOptionsImpl) options));
}
@Override
public APIResponse fetch(Request request, FetchOptions options) {
public APIResponse fetch(Request request, RequestOptions optionsArg) {
RequestOptionsImpl options = (RequestOptionsImpl) optionsArg;
if (options == null) {
options = new FetchOptions();
options = new RequestOptionsImpl();
}
if (options.method == null) {
options.method = request.method();
@@ -59,9 +58,9 @@ class APIRequestContextImpl extends ChannelOwner implements APIRequestContext {
return fetch(request.url(), options);
}
private APIResponse fetchImpl(String url, FetchOptions options) {
private APIResponse fetchImpl(String url, RequestOptionsImpl options) {
if (options == null) {
options = new FetchOptions();
options = new RequestOptionsImpl();
}
JsonObject params = new JsonObject();
params.addProperty("url", url);
@@ -94,10 +93,10 @@ class APIRequestContextImpl extends ChannelOwner implements APIRequestContext {
}
}
if (options.form != null) {
params.add("formData", toNameValueArray(options.form));
params.add("formData", toNameValueArray(options.form.fields));
}
if (options.multipart != null) {
params.add("multipartData", serializeMultipartData(options.multipart));
params.add("multipartData", serializeMultipartData(options.multipart.fields));
}
if (options.timeout != null) {
params.addProperty("timeout", options.timeout);
@@ -151,38 +150,28 @@ class APIRequestContextImpl extends ChannelOwner implements APIRequestContext {
}
@Override
public APIResponse get(String url, GetOptions options) {
FetchOptions fetchOptions = toFetchOptions(options);
fetchOptions.method = "GET";
return fetch(url, fetchOptions);
public APIResponse get(String url, RequestOptions options) {
return fetch(url, ensureOptions(options, "GET"));
}
@Override
public APIResponse head(String url, HeadOptions options) {
FetchOptions fetchOptions = toFetchOptions(options);
fetchOptions.method = "HEAD";
return fetch(url, fetchOptions);
public APIResponse head(String url, RequestOptions options) {
return fetch(url, ensureOptions(options, "HEAD"));
}
@Override
public APIResponse patch(String url, PatchOptions options) {
FetchOptions fetchOptions = toFetchOptions(options);
fetchOptions.method = "PATCH";
return fetch(url, fetchOptions);
public APIResponse patch(String url, RequestOptions options) {
return fetch(url, ensureOptions(options, "PATCH"));
}
@Override
public APIResponse post(String url, PostOptions options) {
FetchOptions fetchOptions = toFetchOptions(options);
fetchOptions.method = "POST";
return fetch(url, fetchOptions);
public APIResponse post(String url, RequestOptions options) {
return fetch(url, ensureOptions(options, "POST"));
}
@Override
public APIResponse put(String url, PutOptions options) {
FetchOptions fetchOptions = toFetchOptions(options);
fetchOptions.method = "PUT";
return fetch(url, fetchOptions);
public APIResponse put(String url, RequestOptions options) {
return fetch(url, ensureOptions(options, "PUT"));
}
@Override
@@ -197,11 +186,14 @@ class APIRequestContextImpl extends ChannelOwner implements APIRequestContext {
});
}
private static <T> FetchOptions toFetchOptions(T options) {
FetchOptions fetchOptions = convertType(options, FetchOptions.class);
if (fetchOptions == null) {
fetchOptions = new FetchOptions();
private static RequestOptionsImpl ensureOptions(RequestOptions options, String method) {
RequestOptionsImpl impl = (RequestOptionsImpl) options;
if (impl == null) {
impl = new RequestOptionsImpl();
}
return fetchOptions;
if (impl.method == null) {
impl.method = method;
}
return impl;
}
}
@@ -0,0 +1,58 @@
/*
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.microsoft.playwright.impl;
import com.microsoft.playwright.options.FilePayload;
import com.microsoft.playwright.options.FormData;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.Map;
public class FormDataImpl implements FormData {
Map<String, Object> fields = new LinkedHashMap<>();
@Override
public FormData set(String name, String value) {
fields.put(name, value);
return this;
}
@Override
public FormData set(String name, boolean value) {
fields.put(name, value);
return this;
}
@Override
public FormData set(String name, int value) {
fields.put(name, value);
return this;
}
@Override
public FormData set(String name, Path value) {
fields.put(name, value);
return this;
}
@Override
public FormData set(String name, FilePayload value) {
fields.put(name, value);
return this;
}
}
@@ -0,0 +1,121 @@
/*
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.microsoft.playwright.impl;
import com.microsoft.playwright.options.FormData;
import com.microsoft.playwright.options.RequestOptions;
import java.util.LinkedHashMap;
import java.util.Map;
public class RequestOptionsImpl implements RequestOptions {
Map<String, Object> params;
String method;
Map<String, String> headers;
Object data;
FormDataImpl form;
FormDataImpl multipart;
Boolean failOnStatusCode;
Boolean ignoreHTTPSErrors;
Double timeout;
@Override
public RequestOptions setHeader(String name, String value) {
if (headers == null) {
headers = new LinkedHashMap<>();
}
headers.put(name, value);
return this;
}
@Override
public RequestOptions setData(String data) {
this.data = data;
return this;
}
@Override
public RequestOptions setData(byte[] data) {
this.data = data;
return this;
}
@Override
public RequestOptions setData(Object data) {
this.data = data;
return this;
}
@Override
public RequestOptions setForm(FormData form) {
this.form = (FormDataImpl) form;
return this;
}
@Override
public RequestOptions setMethod(String method) {
this.method = method;
return this;
}
@Override
public RequestOptions setMultipart(FormData form) {
this.multipart = (FormDataImpl) form;
return this;
}
@Override
public RequestOptions setQueryParam(String name, String value) {
return setQueryParamImpl(name, value);
}
@Override
public RequestOptions setQueryParam(String name, boolean value) {
return setQueryParamImpl(name, value);
}
@Override
public RequestOptions setQueryParam(String name, int value) {
return setQueryParamImpl(name, value);
}
private RequestOptions setQueryParamImpl(String name, Object value) {
if (params == null) {
params = new LinkedHashMap<>();
}
params.put(name, value);
return this;
}
@Override
public RequestOptions setTimeout(double timeout) {
this.timeout = timeout;
return this;
}
@Override
public RequestOptions setFailOnStatusCode(boolean failOnStatusCode) {
this.failOnStatusCode = failOnStatusCode;
return this;
}
@Override
public RequestOptions setIgnoreHTTPSErrors(boolean ignoreHTTPSErrors) {
this.ignoreHTTPSErrors = ignoreHTTPSErrors;
return this;
}
}
@@ -0,0 +1,78 @@
/*
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.microsoft.playwright.options;
import com.microsoft.playwright.impl.FormDataImpl;
import java.nio.file.Path;
import java.util.*;
/**
* The {@code FormData} is used create form data that is sent via {@code APIRequestContext}.
* <pre>{@code
* import com.microsoft.playwright.options.FormData;
* ...
* FormData form = FormData.create()
* .set("firstName", "John")
* .set("lastName", "Doe")
* .set("age", 30);
* page.request().post("http://localhost/submit", RequestOptions.create().setForm(form));
* }</pre>
*/
public interface FormData {
/**
* Creates new instance of {@code FormData}.
*/
static FormData create() {
return new FormDataImpl();
}
/**
* Sets a field on the form. File values can be passed either as {@code Path} or as {@code FilePayload}.
*
* @param name Field name.
* @param value Field value.
*/
FormData set(String name, String value);
/**
* Sets a field on the form. File values can be passed either as {@code Path} or as {@code FilePayload}.
*
* @param name Field name.
* @param value Field value.
*/
FormData set(String name, boolean value);
/**
* Sets a field on the form. File values can be passed either as {@code Path} or as {@code FilePayload}.
*
* @param name Field name.
* @param value Field value.
*/
FormData set(String name, int value);
/**
* Sets a field on the form. File values can be passed either as {@code Path} or as {@code FilePayload}.
*
* @param name Field name.
* @param value Field value.
*/
FormData set(String name, Path value);
/**
* Sets a field on the form. File values can be passed either as {@code Path} or as {@code FilePayload}.
*
* @param name Field name.
* @param value Field value.
*/
FormData set(String name, FilePayload value);
}
@@ -0,0 +1,134 @@
/*
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.microsoft.playwright.options;
import com.microsoft.playwright.impl.RequestOptionsImpl;
import java.util.*;
/**
* The {@code RequestOptions} allows to create form data to be sent via {@code APIRequestContext}.
* <pre>{@code
* context.request().post(
* "https://example.com/submit",
* RequestOptions.create()
* .setQueryParam("page", 1)
* .setData("My data"));
* }</pre>
*/
public interface RequestOptions {
/**
* Creates new instance of {@code RequestOptions}.
*/
static RequestOptions create() {
return new RequestOptionsImpl();
}
/**
* Sets the request's post data.
*
* @param data Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
RequestOptions setData(String data);
/**
* Sets the request's post data.
*
* @param data Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
RequestOptions setData(byte[] data);
/**
* Sets the request's post data.
*
* @param data Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
* {@code content-type} header will be set to {@code application/json} if not explicitly set. Otherwise the {@code content-type} header will
* be set to {@code application/octet-stream} if not explicitly set.
*/
RequestOptions setData(Object data);
/**
*
*
* @param failOnStatusCode Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.
*/
RequestOptions setFailOnStatusCode(boolean failOnStatusCode);
/**
* Provides {@code FormData} object that will be serialized as html form using {@code application/x-www-form-urlencoded} encoding and
* sent as this request body. If this parameter is specified {@code content-type} header will be set to
* {@code application/x-www-form-urlencoded} unless explicitly provided.
*
* @param form Form data to be serialized as html form using {@code application/x-www-form-urlencoded} encoding and sent as this request
* body.
*/
RequestOptions setForm(FormData form);
/**
* Sets an HTTP header to the request.
*
* @param name Header name.
* @param value Header value.
*/
RequestOptions setHeader(String name, String value);
/**
*
*
* @param ignoreHTTPSErrors Whether to ignore HTTPS errors when sending network requests.
*/
RequestOptions setIgnoreHTTPSErrors(boolean ignoreHTTPSErrors);
/**
* Changes the request method (e.g. <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT">PUT</a> or <a
* href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST">POST</a>).
*
* @param method Request method, e.g. <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST">POST</a>.
*/
RequestOptions setMethod(String method);
/**
* Provides {@code FormData} object that will be serialized as html form using {@code multipart/form-data} encoding and sent as this
* request body. If this parameter is specified {@code content-type} header will be set to {@code multipart/form-data} unless
* explicitly provided.
*
* @param form Form data to be serialized as html form using {@code multipart/form-data} encoding and sent as this request body.
*/
RequestOptions setMultipart(FormData form);
/**
* Adds a query parameter to the request URL.
*
* @param name Parameter name.
* @param value Parameter value.
*/
RequestOptions setQueryParam(String name, String value);
/**
* Adds a query parameter to the request URL.
*
* @param name Parameter name.
* @param value Parameter value.
*/
RequestOptions setQueryParam(String name, boolean value);
/**
* Adds a query parameter to the request URL.
*
* @param name Parameter name.
* @param value Parameter value.
*/
RequestOptions setQueryParam(String name, int value);
/**
* Sets request timeout in milliseconds. Defaults to {@code 30000} (30 seconds). Pass {@code 0} to disable timeout.
*
* @param timeout Request timeout in milliseconds.
*/
RequestOptions setTimeout(double timeout);
}
@@ -1,10 +1,7 @@
package com.microsoft.playwright;
import com.google.gson.Gson;
import com.microsoft.playwright.options.Cookie;
import com.microsoft.playwright.options.FilePayload;
import com.microsoft.playwright.options.HttpHeader;
import com.microsoft.playwright.options.SameSiteAttribute;
import com.microsoft.playwright.options.*;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
@@ -128,7 +125,7 @@ public class TestBrowserContextFetch extends TestBase {
void getShouldSupportQueryParams() throws ExecutionException, InterruptedException {
Future<Server.Request> req = server.futureRequest("/empty.html");
context.request().get(server.EMPTY_PAGE + "?p1=foo",
new APIRequestContext.GetOptions().setParams(mapOf("p1", "v1", "парам2", "знач2")));
RequestOptions.create().setQueryParam("p1", "v1").setQueryParam("парам2", "знач2"));
assertNotNull(req.get());
assertEquals("/empty.html?p1=v1&%D0%BF%D0%B0%D1%80%D0%B0%D0%BC2=%D0%B7%D0%BD%D0%B0%D1%872", req.get().url);
}
@@ -138,7 +135,7 @@ public class TestBrowserContextFetch extends TestBase {
@Test
void getShouldSupportFailOnStatusCode() {
try {
context.request().get(server.PREFIX + "/does-not-exist.html", new APIRequestContext.GetOptions().setFailOnStatusCode(true));
context.request().get(server.PREFIX + "/does-not-exist.html", RequestOptions.create().setFailOnStatusCode(true));
fail("did not throw");
} catch (PlaywrightException e) {
assertTrue(e.getMessage().contains("404 Not Found"), e.getMessage());
@@ -148,7 +145,7 @@ public class TestBrowserContextFetch extends TestBase {
@Test
@Disabled("Error: socket hang up")
void getShouldSupportIgnoreHTTPSErrorsOption() {
APIResponse response = context.request().get(httpsServer.EMPTY_PAGE, new APIRequestContext.GetOptions().setIgnoreHTTPSErrors(true));
APIResponse response = context.request().get(httpsServer.EMPTY_PAGE, RequestOptions.create().setIgnoreHTTPSErrors(true));
assertEquals(200, response.status());
}
@@ -163,7 +160,7 @@ public class TestBrowserContextFetch extends TestBase {
cookie.sameSite = SameSiteAttribute.LAX;
context.addCookies(asList(cookie));
Future<Server.Request> req = server.futureRequest("/empty.html");
context.request().get(server.EMPTY_PAGE, new APIRequestContext.GetOptions().setHeaders(mapOf("Cookie", "foo=bar")));
context.request().get(server.EMPTY_PAGE, RequestOptions.create().setHeader("Cookie", "foo=bar"));
assertEquals(asList("foo=bar"), req.get().headers.get("cookie"));
}
@@ -235,9 +232,8 @@ public class TestBrowserContextFetch extends TestBase {
String base64 = Base64.getEncoder().encodeToString("user:pass".getBytes(StandardCharsets.UTF_8));;
Future<Server.Request> request = server.futureRequest("/empty.html");
APIResponse response = context.request().get(server.EMPTY_PAGE, new APIRequestContext.GetOptions().setHeaders(
mapOf("authorization", "Basic " + base64)
));
APIResponse response = context.request().get(server.EMPTY_PAGE, RequestOptions.create()
.setHeader("authorization", "Basic " + base64));
assertEquals(200, response.status());
assertEquals("/empty.html", request.get().url);
}
@@ -269,7 +265,7 @@ public class TestBrowserContextFetch extends TestBase {
void postShouldSupportPostData() throws ExecutionException, InterruptedException {
Future<Server.Request> request = server.futureRequest("/simple.json");
APIResponse response = context.request().post(server.PREFIX + "/simple.json",
new APIRequestContext.PostOptions().setData("My request"));
RequestOptions.create().setData("My request"));
assertEquals("POST", request.get().method);
assertEquals("My request", new String(request.get().postBody));
assertEquals(200, response.status());
@@ -280,7 +276,7 @@ public class TestBrowserContextFetch extends TestBase {
void deleteShouldSupportPostData() throws ExecutionException, InterruptedException {
Future<Server.Request> request = server.futureRequest("/simple.json");
APIResponse response = context.request().delete(server.PREFIX + "/simple.json",
new APIRequestContext.DeleteOptions().setData("My request"));
RequestOptions.create().setData("My request"));
assertEquals("DELETE", request.get().method);
assertEquals("My request", new String(request.get().postBody));
assertEquals(200, response.status());
@@ -291,7 +287,7 @@ public class TestBrowserContextFetch extends TestBase {
void patchShouldSupportPostData() throws ExecutionException, InterruptedException {
Future<Server.Request> request = server.futureRequest("/simple.json");
APIResponse response = context.request().patch(server.PREFIX + "/simple.json",
new APIRequestContext.PatchOptions().setData("My request"));
RequestOptions.create().setData("My request"));
assertEquals("PATCH", request.get().method);
assertEquals("My request", new String(request.get().postBody));
assertEquals(200, response.status());
@@ -302,7 +298,7 @@ public class TestBrowserContextFetch extends TestBase {
void putShouldSupportPostData() throws ExecutionException, InterruptedException {
Future<Server.Request> request = server.futureRequest("/simple.json");
APIResponse response = context.request().put(server.PREFIX + "/simple.json",
new APIRequestContext.PutOptions().setData("My request"));
RequestOptions.create().setData("My request"));
assertEquals("PUT", request.get().method);
assertEquals("My request", new String(request.get().postBody));
assertEquals(200, response.status());
@@ -328,7 +324,7 @@ public class TestBrowserContextFetch extends TestBase {
bytes[i] = (byte) i;
}
Future<Server.Request> request = server.futureRequest("/empty.html");
context.request().post(server.EMPTY_PAGE, new APIRequestContext.PostOptions().setData(bytes));
context.request().post(server.EMPTY_PAGE, RequestOptions.create().setData(bytes));
assertEquals(asList("256"), request.get().headers.get("content-length"));
assertEquals(asList("application/octet-stream"), request.get().headers.get("content-type"));
}
@@ -348,12 +344,11 @@ public class TestBrowserContextFetch extends TestBase {
@Test
void shouldAllowToOverrideDefaultHeaders() throws ExecutionException, InterruptedException {
Future<Server.Request> request = server.futureRequest("/empty.html");
context.request().get(server.EMPTY_PAGE, new APIRequestContext.GetOptions().setHeaders(
mapOf(
"User-Agent", "Playwright",
"Accept", "text/html",
"Accept-Encoding", "br"
)));
context.request().get(server.EMPTY_PAGE, RequestOptions.create()
.setHeader(
"User-Agent", "Playwright")
.setHeader("Accept", "text/html")
.setHeader("Accept-Encoding", "br"));
assertEquals(asList("text/html"), request.get().headers.get("accept"));
assertEquals(asList("Playwright"), request.get().headers.get("user-agent"));
assertEquals(asList("br"), request.get().headers.get("accept-encoding"));
@@ -366,9 +361,8 @@ public class TestBrowserContextFetch extends TestBase {
Future<Server.Request> req1 = server.futureRequest("/a/redirect1");
Future<Server.Request> req2 = server.futureRequest("/b/c/redirect2");
Future<Server.Request> req3 = server.futureRequest("/simple.json");
context.request().get(server.PREFIX + "/a/redirect1", new APIRequestContext.GetOptions().setHeaders(
mapOf("foo", "bar")
));
context.request().get(server.PREFIX + "/a/redirect1",
RequestOptions.create().setHeader("foo", "bar"));
assertEquals(asList("bar"), req1.get().headers.get("foo"));
assertEquals(asList("bar"), req2.get().headers.get("foo"));
assertEquals(asList("bar"), req3.get().headers.get("foo"));
@@ -392,8 +386,8 @@ public class TestBrowserContextFetch extends TestBase {
@Test
void shouldThrowOnInvalidHeaderValue() {
try {
context.request().get(server.EMPTY_PAGE, new APIRequestContext.GetOptions()
.setHeaders(mapOf("foo", "недопустимое значение")));
context.request().get(server.EMPTY_PAGE, RequestOptions.create()
.setHeader("foo", "недопустимое значение"));
fail("did not throw");
} catch (PlaywrightException e) {
assertTrue(e.getMessage().contains("Invalid character in header content"), e.getMessage());
@@ -424,7 +418,7 @@ public class TestBrowserContextFetch extends TestBase {
});
try {
context.request().get(server.PREFIX + "/slow", new APIRequestContext.GetOptions().setTimeout(100));
context.request().get(server.PREFIX + "/slow", RequestOptions.create().setTimeout(100));
fail("did not throw");
} catch (PlaywrightException e) {
assertTrue(e.getMessage().contains("Request timed out after 100ms"), e.getMessage());
@@ -446,7 +440,7 @@ public class TestBrowserContextFetch extends TestBase {
}
});
APIResponse response = context.request().get(server.PREFIX + "/slow",
new APIRequestContext.GetOptions().setTimeout(0));
RequestOptions.create().setTimeout(0));
assertEquals("done", response.text());
}
@@ -497,8 +491,8 @@ public class TestBrowserContextFetch extends TestBase {
void shouldOverrideRequestParameters() throws ExecutionException, InterruptedException {
Request pageReq = page.waitForRequest("**/*", () -> page.navigate(server.EMPTY_PAGE));
Future<Server.Request> req = server.futureRequest("/empty.html");
context.request().fetch(pageReq, new APIRequestContext.FetchOptions().setMethod("POST")
.setHeaders(mapOf("foo", "bar"))
context.request().fetch(pageReq, RequestOptions.create().setMethod("POST")
.setHeader("foo", "bar")
.setData("data"));
assertEquals("POST", req.get().method);
assertEquals(asList("bar"), req.get().headers.get("foo"));
@@ -508,10 +502,11 @@ public class TestBrowserContextFetch extends TestBase {
@Test
void shouldSupportApplicationXWwwFormUrlencoded() throws ExecutionException, InterruptedException {
Future<Server.Request> req = server.futureRequest("/empty.html");
context.request().post(server.EMPTY_PAGE, new APIRequestContext.PostOptions().setForm(
mapOf("firstName", "John",
"lastName", "Doe",
"file", "f.js")));
context.request().post(server.EMPTY_PAGE, RequestOptions.create().setForm(
FormData.create()
.set("firstName", "John")
.set("lastName", "Doe")
.set("file", "f.js")));
assertEquals("POST", req.get().method);
assertEquals(asList("application/x-www-form-urlencoded"), req.get().headers.get("content-type"));
@@ -529,7 +524,7 @@ public class TestBrowserContextFetch extends TestBase {
"file", mapOf("name", "f.js")
);
Future<Server.Request> req = server.futureRequest("/empty.html");
context.request().post(server.EMPTY_PAGE, new APIRequestContext.PostOptions().setData(data));
context.request().post(server.EMPTY_PAGE, RequestOptions.create().setData(data));
assertEquals("POST", req.get().method);
assertEquals(asList("application/json"), req.get().headers.get("content-type"));
String body = new String(req.get().postBody);
@@ -542,11 +537,11 @@ public class TestBrowserContextFetch extends TestBase {
FilePayload file = new FilePayload("f.js", "text/javascript",
"var x = 10;\r\n;console.log(x);".getBytes(StandardCharsets.UTF_8));
APIResponse response = context.request().post(server.EMPTY_PAGE, new APIRequestContext.PostOptions().setMultipart(
mapOf("firstName", "John",
"lastName", "Doe",
"file", file
)));
APIResponse response = context.request().post(server.EMPTY_PAGE, RequestOptions.create().setMultipart(
FormData.create()
.set("firstName", "John")
.set("lastName", "Doe")
.set("file", file)));
assertEquals("POST", serverRequest.get().method);
List<String> contentType = serverRequest.get().headers.get("content-type");
@@ -577,11 +572,11 @@ public class TestBrowserContextFetch extends TestBase {
try (FileOutputStream output = new FileOutputStream(path.toFile())) {
output.write("{\"foo\":\"bar\"}".getBytes(StandardCharsets.UTF_8));
}
APIResponse response = context.request().post(server.EMPTY_PAGE, new APIRequestContext.PostOptions().setMultipart(
mapOf("firstName", "John",
"lastName", "Doe",
"file", path
)));
APIResponse response = context.request().post(server.EMPTY_PAGE, RequestOptions.create().setMultipart(
FormData.create()
.set("firstName", "John")
.set("lastName", "Doe")
.set("file", path)));
assertEquals("POST", serverRequest.get().method);
List<String> contentType = serverRequest.get().headers.get("content-type");
@@ -609,8 +604,8 @@ public class TestBrowserContextFetch extends TestBase {
"firstName", "John",
"lastName", "Doe");
Future<Server.Request> req = server.futureRequest("/empty.html");
context.request().post(server.EMPTY_PAGE, new APIRequestContext.PostOptions()
.setHeaders(mapOf("content-type", "unknown"))
context.request().post(server.EMPTY_PAGE, RequestOptions.create()
.setHeader("content-type", "unknown")
.setData(data));
assertEquals("POST", req.get().method);
assertEquals(asList("unknown"), req.get().headers.get("content-type"));
@@ -621,7 +616,7 @@ public class TestBrowserContextFetch extends TestBase {
@Test
void shouldThrowWhenDataPassedForUnsupportedRequest() {
try {
context.request().fetch(server.EMPTY_PAGE, new APIRequestContext.FetchOptions()
context.request().fetch(server.EMPTY_PAGE, RequestOptions.create()
.setMethod("GET").setData("bar"));
fail("did not throw");
} catch (PlaywrightException e) {
@@ -650,13 +645,12 @@ public class TestBrowserContextFetch extends TestBase {
@Test
void shouldAcceptBoolAndNumericParams() throws ExecutionException, InterruptedException {
Future<Server.Request> req = server.futureRequest("/empty.html");
page.request().get(server.EMPTY_PAGE, new APIRequestContext.GetOptions().setParams(mapOf(
"str", "s",
"num", 10,
"bool", true,
"bool2", false
)));
assertEquals("/empty.html?str=s&bool2=false&bool=true&num=10", req.get().url);
page.request().get(server.EMPTY_PAGE, RequestOptions.create()
.setQueryParam("str", "s")
.setQueryParam("num", 10)
.setQueryParam("bool", true)
.setQueryParam("bool2", false));
assertEquals("/empty.html?str=s&num=10&bool=true&bool2=false", req.get().url);
}
@Test
@@ -2,6 +2,7 @@ package com.microsoft.playwright;
import com.google.gson.Gson;
import com.microsoft.playwright.options.HttpHeader;
import com.microsoft.playwright.options.RequestOptions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@@ -212,7 +213,7 @@ public class TestGlobalFetch extends TestBase {
void shouldPropagateIgnoreHTTPSErrorsOnRedirects() {
httpsServer.setRedirect("/redir", "/empty.html");
APIRequestContext request = playwright.request().newContext();
APIResponse response = request.get(httpsServer.PREFIX + "/redir", new APIRequestContext.GetOptions().setIgnoreHTTPSErrors(true));
APIResponse response = request.get(httpsServer.PREFIX + "/redir", RequestOptions.create().setIgnoreHTTPSErrors(true));
assertEquals(200, response.status());
}
@@ -259,7 +260,7 @@ public class TestGlobalFetch extends TestBase {
APIRequestContext request = playwright.request().newContext();
Future<Server.Request> req1 = server.futureRequest("/redirect");
Future<Server.Request> req2 = server.futureRequest("/empty.html");
APIResponse result = request.post(server.PREFIX + "/redirect", new APIRequestContext.PostOptions().setData(mapOf("foo", "bar")));
APIResponse result = request.post(server.PREFIX + "/redirect", RequestOptions.create().setData(mapOf("foo", "bar")));
assertEquals(200, result.status());
assertEquals(asList("13"), req1.get().headers.get("content-length"));
@@ -280,7 +281,7 @@ public class TestGlobalFetch extends TestBase {
APIRequestContext request = playwright.request().newContext();
for (Object value : values) {
Future<Server.Request> req = server.futureRequest("/empty.html");
request.post(server.EMPTY_PAGE, new APIRequestContext.PostOptions().setHeaders(mapOf("content-type", "application/json")).setData(value));
request.post(server.EMPTY_PAGE, RequestOptions.create().setHeader("content-type", "application/json").setData(value));
byte[] body = req.get().postBody;
assertEquals(new Gson().toJson(value), new String(body));
}
@@ -293,8 +294,8 @@ public class TestGlobalFetch extends TestBase {
for (Object value : values) {
String stringifiedValue = new Gson().toJson(value);
Future<Server.Request> req = server.futureRequest("/empty.html");
request.post(server.EMPTY_PAGE, new APIRequestContext.PostOptions()
.setHeaders(mapOf("content-type", "application/json"))
request.post(server.EMPTY_PAGE, RequestOptions.create()
.setHeader("content-type", "application/json")
.setData(stringifiedValue));
byte[] body = req.get().postBody;
assertEquals(stringifiedValue, new String(body));
+1 -1
View File
@@ -1 +1 @@
1.18.0-alpha-1637257604000
1.18.0-alpha-1637368835000
@@ -268,6 +268,7 @@ class TypeRef extends Element {
customTypeNames.put("Frame.setInputFiles.files", "FilePayload");
customTypeNames.put("Page.setInputFiles.files", "FilePayload");
customTypeNames.put("Page.setInputFiles.files", "FilePayload");
customTypeNames.put("FormData.set.value", "FilePayload");
customTypeNames.put("Page.dragAndDrop.options.sourcePosition", "Position");
customTypeNames.put("Frame.dragAndDrop.options.sourcePosition", "Position");
@@ -630,6 +631,20 @@ class Method extends Element {
output.add(offset + "}");
return;
}
if ("FormData.create".equals(jsonPath)) {
writeJavadoc(params, output, offset);
output.add(offset + "static FormData create() {");
output.add(offset + " return new FormDataImpl();");
output.add(offset + "}");
return;
}
if ("RequestOptions.create".equals(jsonPath)) {
writeJavadoc(params, output, offset);
output.add(offset + "static RequestOptions create() {");
output.add(offset + " return new RequestOptionsImpl();");
output.add(offset + "}");
return;
}
if ("PlaywrightAssertions.assertThat".equals(jsonPath)) {
writeJavadoc(params, output, offset);
String originalName = jsonElement.getAsJsonObject().get("originalName").getAsString();
@@ -896,10 +911,10 @@ class Interface extends TypeDefinition {
}
void writeTo(List<String> output, String offset) {
if ("Playwright".equals(jsonName)) {
output.add("import com.microsoft.playwright.impl.PlaywrightImpl;");
if (methods.stream().anyMatch(m -> "create".equals(m.jsonName))) {
output.add("import com.microsoft.playwright.impl." + jsonName + "Impl;");
}
if (asList("Page", "Request", "Response", "APIRequest", "APIResponse", "FileChooser", "Frame", "ElementHandle", "Locator", "Browser", "BrowserContext", "BrowserType", "Mouse", "Keyboard").contains(jsonName)) {
if (asList("Page", "Request", "Response", "APIRequestContext", "APIRequest", "APIResponse", "FileChooser", "Frame", "ElementHandle", "Locator", "Browser", "BrowserContext", "BrowserType", "Mouse", "Keyboard").contains(jsonName)) {
output.add("import com.microsoft.playwright.options.*;");
}
if (jsonName.equals("Route")) {
@@ -908,7 +923,7 @@ class Interface extends TypeDefinition {
if ("Download".equals(jsonName)) {
output.add("import java.io.InputStream;");
}
if (asList("Page", "Frame", "ElementHandle", "Locator", "APIRequest", "APIRequestContext", "FileChooser", "Browser", "BrowserContext", "BrowserType", "Download", "Route", "Selectors", "Tracing", "Video").contains(jsonName)) {
if (asList("Page", "Frame", "ElementHandle", "Locator", "FormData", "APIRequest", "APIRequestContext", "FileChooser", "Browser", "BrowserContext", "BrowserType", "Download", "Route", "Selectors", "Tracing", "Video").contains(jsonName)) {
output.add("import java.nio.file.Path;");
}
output.add("import java.util.*;");
@@ -1118,11 +1133,16 @@ public class ApiGenerator {
if (!classFilter.test(name)) {
continue;
}
Interface iface = new Interface(entry.getAsJsonObject(), topLevelTypes);
if (asList("RequestOptions", "FormData").contains(name)) {
topLevelTypes.put(name, iface);
continue;
}
List<String> lines = new ArrayList<>();
lines.add(Interface.header);
lines.add("package " + packageName + ";");
lines.add("");
new Interface(entry.getAsJsonObject(), topLevelTypes).writeTo(lines, "");
iface.writeTo(lines, "");
String text = String.join("\n", lines);
try (FileWriter writer = new FileWriter(new File(dir, name + ".java"))) {
writer.write(text);