Class SHAFT.API
java.lang.Object
com.shaft.driver.SHAFT.API
- Enclosing class:
SHAFT
Manages a REST API session and exposes a fluent interface for building
and executing HTTP requests (GET, POST, PUT, PATCH, DELETE).
Usage example:
SHAFT.API api = new SHAFT.API("https://jsonplaceholder.typicode.com");
api.get("/posts/1").setTargetStatusCode(200);
api.assertThatResponse().extractedJsonValue("$.title").isNotNull();
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionAPI(RestActions existingSession) Wraps an existingRestActionssession for continued use.Creates a new API session pointing at the given base service URI.Creates a new API session pointing at the given base service URI, seeded with cookies read from a SHAFT browser storage-state JSON file (the same file produced bydriver.browser().saveStorageState(String)). -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a persistent cookie that will be sent with every subsequent request in this session.voidAdds a persistent header that will be sent with every subsequent request in this session.Starts building a hard assertion against the last API response.Builds a DELETE request for the specified service endpoint.exportCookiesTo(BrowserActionsContract browserActions) Exports all cookies from this API session into the given browser session.exportCookiesTo(BrowserActionsContract browserActions, String domainFilter, String pathFilter) Exports API session cookies that match the optional domain and path filters into the given browser session.Builds a GET request for the specified service endpoint.Returns an immutable snapshot of cookies attached to this API session.Returns an immutable snapshot of headers attached to this API session.static SHAFT.APIgetInstance(String serviceURI) Factory method equivalent tonew API(serviceURI).io.restassured.response.ResponseReturns the raw REST AssuredResponsefrom the most recent request.<T> TgetResponseAs(com.fasterxml.jackson.core.type.TypeReference<T> typeReference) Maps the most recent JSON response body to a generic Java type.<T> TgetResponseAs(Class<T> responseType) Maps the most recent JSON response body to the requested Java type.<T> List<T> getResponseAsList(Class<T> elementType) Maps the most recent JSON array response body to a list of the requested Java type.Returns the response body of the most recent request as a string.getResponseJSONValue(String jsonPath) Extracts a value from the most recent JSON response using a JSONPath expression.getResponseJSONValueAsList(String jsonPath) Extracts a list of values from the most recent JSON response using a JSONPath expression.intReturns the HTTP status code of the most recent response.longReturns the response time in milliseconds for the most recent request.getResponseXMLValue(String xmlPath) Extracts a value from the most recent XML response using an XPath expression.getResponseXMLValueAsList(String xmlPath) Extracts a list of values from the most recent XML response using an XPath expression.Builds a HEAD request for the specified service endpoint.importCookiesFrom(BrowserActionsContract browserActions) Imports all browser cookies into this API session.importCookiesFrom(BrowserActionsContract browserActions, String domainFilter, String pathFilter) Imports browser cookies that match the optional domain and path filters into this API session.Builds an OPTIONS request for the specified service endpoint, typically used to inspect the allowed methods (CORS/preflight) an endpoint exposes.Builds a PATCH request for the specified service endpoint.Builds a POST request for the specified service endpoint.Builds a PUT request for the specified service endpoint.sendGraphQlRequest(String serviceName, String query) Builds a GraphQL POST request with a JSON body containing only the supplied query.sendGraphQlRequest(String serviceName, String query, Object variables) Builds a GraphQL POST request with query and variables.sendGraphQlRequest(String serviceName, String query, Object variables, String fragment) Builds a GraphQL POST request with query, variables, and fragment.Starts building a soft verification against the last API response.
-
Constructor Details
-
API
Creates a new API session pointing at the given base service URI.- Parameters:
serviceURI- the base URI of the target web service (e.g.,"https://api.example.com")
-
API
Creates a new API session pointing at the given base service URI, seeded with cookies read from a SHAFT browser storage-state JSON file (the same file produced bydriver.browser().saveStorageState(String)).- Parameters:
serviceURI- the base URI of the target web service (e.g.,"https://api.example.com")storageStatePath- path to a storage-state JSON file previously saved from a browser session
-
API
Wraps an existingRestActionssession for continued use.- Parameters:
existingSession- an already-initialised REST session
-
-
Method Details
-
getInstance
-
get
Builds a GET request for the specified service endpoint.- Parameters:
serviceName- the endpoint path (appended to the base URI)- Returns:
- a
RequestBuilderfor further request configuration
-
post
Builds a POST request for the specified service endpoint.- Parameters:
serviceName- the endpoint path (appended to the base URI)- Returns:
- a
RequestBuilderfor further request configuration
-
patch
Builds a PATCH request for the specified service endpoint.- Parameters:
serviceName- the endpoint path (appended to the base URI)- Returns:
- a
RequestBuilderfor further request configuration
-
delete
Builds a DELETE request for the specified service endpoint.- Parameters:
serviceName- the endpoint path (appended to the base URI)- Returns:
- a
RequestBuilderfor further request configuration
-
put
Builds a PUT request for the specified service endpoint.- Parameters:
serviceName- the endpoint path (appended to the base URI)- Returns:
- a
RequestBuilderfor further request configuration
-
head
Builds a HEAD request for the specified service endpoint. HEAD returns the same headers/status as GET without a response body, so it is useful for existence, cache, and header assertions.- Parameters:
serviceName- the endpoint path (appended to the base URI)- Returns:
- a
RequestBuilderfor further request configuration
-
options
Builds an OPTIONS request for the specified service endpoint, typically used to inspect the allowed methods (CORS/preflight) an endpoint exposes.- Parameters:
serviceName- the endpoint path (appended to the base URI)- Returns:
- a
RequestBuilderfor further request configuration
-
sendGraphQlRequest
Builds a GraphQL POST request with a JSON body containing only the supplied query.- Parameters:
serviceName- the GraphQL endpoint path appended to the base URIquery- the GraphQL query or mutation- Returns:
- a
RequestBuilderfor further request configuration
-
sendGraphQlRequest
Builds a GraphQL POST request with query and variables.- Parameters:
serviceName- the GraphQL endpoint path appended to the base URIquery- the GraphQL query or mutationvariables- GraphQL variables, commonly aMapor JSON string- Returns:
- a
RequestBuilderfor further request configuration
-
sendGraphQlRequest
public RequestBuilder sendGraphQlRequest(String serviceName, String query, Object variables, String fragment) Builds a GraphQL POST request with query, variables, and fragment.- Parameters:
serviceName- the GraphQL endpoint path appended to the base URIquery- the GraphQL query or mutationvariables- GraphQL variables, commonly aMapor JSON stringfragment- reusable GraphQL fragment content- Returns:
- a
RequestBuilderfor further request configuration
-
addHeader
-
addCookie
-
importCookiesFrom
Imports all browser cookies into this API session.- Parameters:
browserActions- browser actions facade to read cookies from- Returns:
- this API session for fluent chaining
-
importCookiesFrom
public SHAFT.API importCookiesFrom(BrowserActionsContract browserActions, String domainFilter, String pathFilter) Imports browser cookies that match the optional domain and path filters into this API session.- Parameters:
browserActions- browser actions facade to read cookies fromdomainFilter- optional exact domain filter; passnullor blank to import every domainpathFilter- optional exact path filter; passnullor blank to import every path- Returns:
- this API session for fluent chaining
-
exportCookiesTo
Exports all cookies from this API session into the given browser session.- Parameters:
browserActions- browser actions facade to write cookies into- Returns:
- this API session for fluent chaining
-
exportCookiesTo
public SHAFT.API exportCookiesTo(BrowserActionsContract browserActions, String domainFilter, String pathFilter) Exports API session cookies that match the optional domain and path filters into the given browser session.- Parameters:
browserActions- browser actions facade to write cookies intodomainFilter- optional exact domain filter; passnullor blank to export every domainpathFilter- optional exact path filter; passnullor blank to export every path- Returns:
- this API session for fluent chaining
-
getCookies
-
getHeaders
-
assertThatResponse
Starts building a hard assertion against the last API response.- Returns:
- a
RestValidationsBuilderfor response assertions
-
verifyThatResponse
Starts building a soft verification against the last API response.- Returns:
- a
RestValidationsBuilderfor response verifications
-
getResponse
public io.restassured.response.Response getResponse()Returns the raw REST AssuredResponsefrom the most recent request.- Returns:
- the last
Response
-
getResponseBody
Returns the response body of the most recent request as a string.- Returns:
- the response body text
-
getResponseAs
Maps the most recent JSON response body to the requested Java type.- Type Parameters:
T- the mapped response type- Parameters:
responseType- the target class, record, or POJO type- Returns:
- the response body mapped to
responseType - Throws:
NullPointerException- ifresponseTypeisnullIllegalStateException- if the response body is empty or the response is not JSONIllegalArgumentException- if the response body cannot be mapped toresponseType
-
getResponseAsList
Maps the most recent JSON array response body to a list of the requested Java type.- Type Parameters:
T- the mapped list element type- Parameters:
elementType- the target class, record, or POJO type for each array item- Returns:
- the response body mapped to a
List - Throws:
NullPointerException- ifelementTypeisnullIllegalStateException- if the response body is empty or the response is not JSONIllegalArgumentException- if the response body cannot be mapped toList<elementType>
-
getResponseAs
public <T> T getResponseAs(com.fasterxml.jackson.core.type.TypeReference<T> typeReference) Maps the most recent JSON response body to a generic Java type.- Type Parameters:
T- the mapped response type- Parameters:
typeReference- the target generic type reference- Returns:
- the response body mapped to
typeReference - Throws:
NullPointerException- iftypeReferenceisnullIllegalStateException- if the response body is empty or the response is not JSONIllegalArgumentException- if the response body cannot be mapped totypeReference
-
getResponseStatusCode
public int getResponseStatusCode()Returns the HTTP status code of the most recent response.- Returns:
- the status code (e.g., 200, 404)
-
getResponseTime
public long getResponseTime()Returns the response time in milliseconds for the most recent request.- Returns:
- the response time in milliseconds
-
getResponseJSONValue
-
getResponseJSONValueAsList
-
getResponseXMLValue
-
getResponseXMLValueAsList
-