Class Api
For one instance run recommended: Api api = api.getInstance();
This client provides a convenient fluent API for:
- Sending GET, POST, PUT and DELETE requests
- Configuring request headers/query params
- Using Basic or Bearer authentication
- Enabling request/response logging
- Support BugReaper YML config
-
Field Summary
Fields inherited from class net.bugreaper.modules.api.setup.ApiAbstract
authToken, contentType, enableLogging, headers, logger, maxResponseMsAssert, maxTimeoutMs, password, port, queryParams, requestHeaders, requestQueryParams, url, useBasicAuth, username -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionRemove global headersRemove global query parametersReturns and logs (at INFO level) a human-readable summary of all resolved configuration values.static ApisendDelete(String path) Send DELETE request without bodysendDelete(String path, Object body) Send DELETE request with bodySend GET requestSend HEAD requestsendOptions(String path) Send OPTIONS requestSend PATCH request with bodySend POST request without bodySend POST request with bodySend PUT request without bodySend PUT request with bodysetBasicAuth(String username, String password) Sets Basic authenticationsetBearerAuth(String token) Sets Bearer authenticationsetContentType(io.restassured.http.ContentType contentType) Set content type manuallyContentTypeSet content type JSONSet content type XMLAdd or updates header globally (add to previously set)setHeaders(Map<String, Object> headers) Set headers globally (overwrite previously set)setLogging(boolean enable) Enables or disables logging manually (debug log level will print logs anyway!)setMaxResponseMsAssert(int maxResponseMs) Set maxResponseTimeout (does not break the connection - only assert)Unset all authenticationsSet content type absentsetQueryParams(Map<String, Object> queryParams) Set query parameters globally (overwrite previously set)setTimeoutMs(int maxTimeoutMs) Set max request timeout(connection/read)withHeader(String key, Object value) Add header for one next request (global headers will be ignored) use for specific request can be used multiple times to add multiple headers after request global headers are taken into account againwithHeaders(Map<String, Object> queryParams) Add headers for one next request (global headers will be ignored) use for specific request after request global headers are taken into account againwithQueryParam(String key, Object value) Add query parameter once for next request (global query parameters will be ignored) use before specific request can be used multiple times to add multiple query parameters after request global query parameter are taken into account againwithQueryParams(Map<String, Object> queryParams) Add query parameters once for next request (global query parameters will be ignored) use before specific request after request global query parameter are taken into account againMethods inherited from class net.bugreaper.modules.api.setup.ApiAbstract
buildRequest, buildSimpleRequest
-
Constructor Details
-
Api
This constructor initializes client for interaction with API- Parameters:
url- service url ("http://my-service")port- service port
-
Api
public Api()Constructs api client configuration.Loads configuration values from a YAML file.
Default file:
bugreaper.ymlCustom file: using
-DbugreaperEnv=testloadsbugreaper-test.ymlmodules: api: url: http://localhost port: 1082 username: user_1 # optional password: pass123 # optional token: my-token # optional more priority than base auth! logging: true # optional max-response-ms-assert: 2000 # optional max-timeout-ms: 1000 # optionalMissing required keys will result in configuration errors. Missing optional keys will fall back to predefined defaults.
-
Api
Constructs api client configuration.- Parameters:
suffix- concatenation of Api client (example Api("-2") keys will be "modules.api-2")Loads configuration values from a YAML file.
Default file:
bugreaper.ymlCustom file: using
-DbugreaperEnv=testloadsbugreaper-test.ymlmodules: api{suffix}: # example new Api("-2") key will be "api-2" url: http://localhost port: 8080 username: user_2 # optional password: pass123 # optional token: my-token # optional more priority than base auth! logging: true # optional max-response-ms-assert: 2000 # optional max-timeout-ms: 1000 # optionalMissing required keys will result in configuration errors. Missing optional keys will fall back to predefined defaults.
-
-
Method Details
-
getInstance
Returns the instance ofApiwith config builderApi().This implementation is thread-safe using method-level synchronization.
- Returns:
- the singleton instance of
Api - See Also:
-
setContentTypeJson
Description copied from interface:ApiConfigSet content type JSON- Specified by:
setContentTypeJsonin interfaceApiConfig- Returns:
- this instance for method chaining
-
setContentTypeXml
Description copied from interface:ApiConfigSet content type XML- Specified by:
setContentTypeXmlin interfaceApiConfig- Returns:
- this instance for method chaining
-
setContentType
Description copied from interface:ApiConfigSet content type manuallyContentType- Specified by:
setContentTypein interfaceApiConfig- Parameters:
contentType-ContentType:ContentType.ANYContentType.TEXTContentType.JSONContentType.XMLContentType.HTMLContentType.URLENCContentType.BINARYContentType.MULTIPART
- Returns:
- this instance for method chaining
-
setMaxResponseMsAssert
Description copied from interface:ApiConfigSet maxResponseTimeout (does not break the connection - only assert)- Specified by:
setMaxResponseMsAssertin interfaceApiConfig- Parameters:
maxResponseMs- assert ms for max response time- Returns:
- this instance for method chaining
-
setTimeoutMs
Description copied from interface:ApiConfigSet max request timeout(connection/read)Drop connection on timeout is up
- Specified by:
setTimeoutMsin interfaceApiConfig- Parameters:
maxTimeoutMs- max ms for connection/read- Returns:
- this instance for method chaining
-
setNoContentType
Description copied from interface:ApiConfigSet content type absent- Specified by:
setNoContentTypein interfaceApiConfig- Returns:
- this instance for method chaining
-
setHeader
Description copied from interface:ApiConfigAdd or updates header globally (add to previously set) -
setHeaders
Description copied from interface:ApiConfigSet headers globally (overwrite previously set)- Specified by:
setHeadersin interfaceApiConfig- Parameters:
headers- Map (key, value)- Returns:
- this instance for method chaining
-
cleanSetHeaders
Description copied from interface:ApiConfigRemove global headers- Specified by:
cleanSetHeadersin interfaceApiConfig- Returns:
- this instance for method chaining
-
withHeader
Description copied from interface:ApiConfigAdd header for one next request (global headers will be ignored)- use for specific request
- can be used multiple times to add multiple headers
- after request global headers are taken into account again
- Specified by:
withHeaderin interfaceApiConfig- Parameters:
key- header keyvalue- header value- Returns:
- this instance for method chaining
-
withHeaders
Description copied from interface:ApiConfigAdd headers for one next request (global headers will be ignored)- use for specific request
- after request global headers are taken into account again
- Specified by:
withHeadersin interfaceApiConfig- Parameters:
queryParams- Map (key, value)- Returns:
- this instance for method chaining
-
setQueryParams
Description copied from interface:ApiConfigSet query parameters globally (overwrite previously set)- Specified by:
setQueryParamsin interfaceApiConfig- Parameters:
queryParams- Map (key, value)- Returns:
- this instance for method chaining
-
cleanSetQueryParams
Description copied from interface:ApiConfigRemove global query parameters- Specified by:
cleanSetQueryParamsin interfaceApiConfig- Returns:
- this instance for method chaining
-
withQueryParam
Description copied from interface:ApiConfigAdd query parameter once for next request (global query parameters will be ignored)- use before specific request
- can be used multiple times to add multiple query parameters
- after request global query parameter are taken into account again
- Specified by:
withQueryParamin interfaceApiConfig- Parameters:
key- parameter keyvalue- parameter value- Returns:
- this instance for method chaining
-
withQueryParams
Description copied from interface:ApiConfigAdd query parameters once for next request (global query parameters will be ignored)- use before specific request
- after request global query parameter are taken into account again
- Specified by:
withQueryParamsin interfaceApiConfig- Parameters:
queryParams- Map (key, value)- Returns:
- this instance for method chaining
-
setBearerAuth
Description copied from interface:ApiConfigSets Bearer authentication- Specified by:
setBearerAuthin interfaceApiConfig- Parameters:
token- String with token- Returns:
- this instance for method chaining
-
setBasicAuth
Description copied from interface:ApiConfigSets Basic authentication- Specified by:
setBasicAuthin interfaceApiConfig- Parameters:
username- String with usernamepassword- String with password- Returns:
- this instance for method chaining
-
setNoAuth
Description copied from interface:ApiConfigUnset all authentications -
setLogging
Description copied from interface:ApiConfigEnables or disables logging manually (debug log level will print logs anyway!)- Specified by:
setLoggingin interfaceApiConfig- Parameters:
enable- true=request/response logging (Allure on always!)- Returns:
- this instance for method chaining
-
getConfigSummary
Description copied from interface:ApiConfigReturns and logs (at INFO level) a human-readable summary of all resolved configuration values.The summary includes values loaded from the YAML configuration file as well as any fields overridden programmatically after construction. Optional fields that were not present in the configuration and resolved via default values may also be included.
- Specified by:
getConfigSummaryin interfaceApiConfig- Returns:
- String with summary
-
sendGet
Description copied from interface:ApiIntSend GET request- Specified by:
sendGetin interfaceApiInt- Parameters:
path- String with path (/api/user1)- Returns:
AssertableResponse
-
sendHead
Description copied from interface:ApiIntSend HEAD request- Specified by:
sendHeadin interfaceApiInt- Parameters:
path- String with path (/api/user1)- Returns:
AssertableResponse
-
sendOptions
Description copied from interface:ApiIntSend OPTIONS request- Specified by:
sendOptionsin interfaceApiInt- Parameters:
path- String with path (/api/user1)- Returns:
AssertableResponse
-
sendPost
Description copied from interface:ApiIntSend POST request with body- Specified by:
sendPostin interfaceApiInt- Parameters:
path- String with path (/api/user1)body- String with body- Returns:
AssertableResponse
-
sendPost
Description copied from interface:ApiIntSend POST request without body- Specified by:
sendPostin interfaceApiInt- Parameters:
path- String with path (/api/user1)- Returns:
AssertableResponse
-
sendPut
Description copied from interface:ApiIntSend PUT request with body- Specified by:
sendPutin interfaceApiInt- Parameters:
path- String with path (/api/user1)body- String with body- Returns:
AssertableResponse
-
sendPut
Description copied from interface:ApiIntSend PUT request without body- Specified by:
sendPutin interfaceApiInt- Parameters:
path- String with path (/api/user1)- Returns:
AssertableResponse
-
sendPatch
Description copied from interface:ApiIntSend PATCH request with body- Specified by:
sendPatchin interfaceApiInt- Parameters:
path- String with path (/api/user1)body- String with body- Returns:
AssertableResponse
-
sendDelete
Description copied from interface:ApiIntSend DELETE request with body- Specified by:
sendDeletein interfaceApiInt- Parameters:
path- String with path (/api/user1)body- String with body- Returns:
AssertableResponse
-
sendDelete
Description copied from interface:ApiIntSend DELETE request without body- Specified by:
sendDeletein interfaceApiInt- Parameters:
path- String with path (/api/user1)- Returns:
AssertableResponse
-