Skip to main content

Middleware

Middleware

Restel, can help you solve more woes with the help of middleware. Restel middleware provides us an opportunity to add pre or post processor to the requests and response.

How to use middleware feature?

In test definition sheet two columns are earmarked for middleware as follows:

  • request_pre_call_hook
  • request_post_call_hook

as the name suggests former one is used for pre-processing and the later is used in post processing. Wondering what kind of processing would require while testing? We often bump into the requirements of storing the response into a file for debugging or for comparisons and the most important aspect in today's context is authentication & authorization.

Request Middleware

At this moment we Restel supports three request middleware:

  • BasicAuth Middleware
  • Oauth2 Client credential Middleware
  • Oauth2 Resource Owner Password Middleware

BasicAuth Middleware

If you're dealing with APIs that supports BasicAuth then you should probably use this middleware which adds Authorization header to the request headers with the given username and password.

This middleware adds the header Authorization: Basic <token> to the request, where token is the Base64 encoding of username:password.

{
"Authorization": {
"basic_auth": {
"username": "<username>",
"password": "<password>"
}
}
}
FieldMandatory/OptionalFormatExample
usernameMandatoryStringBilly
passwordMandatoryStringThisIsACrazyPassword

OAuth2 Client Credential Middleware

OAuth based authentication has become a de facto standard for most of the application these days and if your application is relying on OAuth2 with client credentials then use this middleware it adds Authorization header to the request headers which make calls to the authorization server to fetch accessToken which has Oauth2 authentication with grant type client credentials.. Header Key : Authorization , Value : Bearer <accesstoken> . Where accesstoken is a token generated by the Authorization Server.

{
"Authorization": {
"oauth2": {
"client_credentials": {
"authUrl": "<auth_url>",
"clientId": "<client_id>",
"clientSecret": "<clientSecret>",
"scope": "<Scopes>"
}
}
}
}

FieldMandatory/OptionalFormatExample
authUrlMandatoryStringhttps://authserver.com/v1/token
clientSecretMandatoryString8A6eAB8hUjOb9w5hWCT6CndX5FY0gFomfRMvv65jDON'TEXPOSEYOURSECERETSANYWHERE
clientIdMandatoryStringYOURCLIENTIDLIKE0oa10nhj6gkhyPLXb4x9
scopeOptionalSpace separated StringemailId personal groups
passwordMandatoryStringThisIsACrazyPassword

OAuth2 Resource Owner Password Middleware

If you're using resource owner password while invoking the application then use this middleware it adds Authorization header to the request headers which make calls to the authorization server to fetch accessToken which has Oauth2 authentication with grant type password.. Header Key : Authorization , Value : Bearer <accesstoken> . Where accesstoken is a token generated by the Authorization Server.

{
"Authorization": {
"oauth2": {
"password": {
"username": "<username>",
"password": "<password>",
"authUrl": "<auth_url>",
"clientId": "<client_id>",
"clientSecret": "<clientSecret>",
"scope": "<Scopes>"
}
}
}
}
FieldMandatory/OptionalFormatExample
usernameMandatoryStringBilly
passwordMandatoryStringThisIsACrazyPassword
authUrlMandatoryStringhttps://authserver.com/v1/token
clientSecretMandatoryString8A6eAB8hUjOb9w5hWCT6CndX5FY0gFomfRMvv65jDON'TEXPOSEYOURSECERETSANYWHERE
clientIdMandatoryStringYOURCLIENTIDLIKE0oa10nhj6gkhyPLXb4x9
scopeOptionalSpace separated StringemailId personal groups
passwordMandatoryStringThisIsACrazyPassword

Response Middleware

Response Writer Middleware

If you want required to record the response in a file then use this middleware it will write the response payload to the local file. Should provide the path to where the response has to be written.

{
"write": “<filePath>”
}