Document alternative request bodies - apiblueprint

The API we are building allows the user to provide different request bodies. From what I have seen / found so far, it is only possible to provide a single request per action/resource. Is it somehow possible to specify different request bodies?
I tried it like:
### Create new line item for an existing order [POST]
Some description
+ Request (application/json)
### Create new line item and order [POST]
Some other description
+ Request (application/json)
And also like:
### Create new line item for an existing order [POST]
Some description
+ Request (application/json)
+ Request (application/json)

You can have multiple request and responses in a single action. Please see the example below.
### Create new line item for an existing order [POST]
Some description
+ Request (application/json)
+ Response 200
+ Request (application/json)
+ Response 404
+ Request (application/xml)
+ Request (application/json)
+ Response 200

Related

Jmeter log only HTTPSampler with JSR223 Listener

Trying to have a JSR223 Listener on Test Plan level so it listens to all thread groups. What I like to achieve is that it only logs every HTTP Request's methods (GET, POST, HEAD, OPTIONS, PUT, DELETE and TRACE) and print every Method, URL, Response Code, Value and Response Data.
Having looked around, found some snippets left right and center and have come up with this:
try{
if (sampler.getUrl().toString().contains("http")){
log.warn('Method: ' + sampler.getMethod() + ' URL: ' + sampler.getUrl().toString());
sampler.getArguments().each {arg ->
log.warn('CODE: ' + prev.getResponseCode() + ' Value: ' + arg.getStringValue())
}
log.warn('CODE: ' + prev.getResponseCode() + ' ResponseData: ' + prev.getResponseDataAsString());
}else{
log.warn('Not a HTTPRequest')
}
}
catch (Throwable ex)
{
log.warn('Something went wrong', ex);
throw ex;
}
And I get the results in my log viewer as desired.
The challenge I am facing is if any other sampler type is active, it logs an error because that sampler is not an HTTP request but a dummy sampler, so it cannot return the requested values.
groovy.lang.MissingMethodException: No signature of method: kg.apc.jmeter.samplers.DummySampler.getUrl()
Now my example shows only two samplers in the Thread Group, but imagine a couple of 100 various samplers
Figuring I need to change my 'if' statement to not check for the sampler argument, but for the sampler type instead (HTTPRequest), but good old google returns not much to go on. So, is it even possible to have an if statement that checks for sampler type, and what would that syntax be?
if (sampler==HTTPRequest) ???
You can use sampler.getClass() and compare it to check if HTTP Request:
if("HTTPSamplerProxy".equals(sampler.getClass().getSimpleName())) {
You can check class name using log:
log.info(sampler.getClass().getSimpleName());

Apiary Blueprint attributes under headers and body are not recognized

Final Edit: This works with no semantic errors:
+ Request
+ Headers
Accept: application/json
Content-Type: application/json
X-Auth-Client: Your Client Id
X-Auth-Token: Your Token
+ Body
+ Attributes (ProductPost)
+ Response 200
+ Headers
Content-Encoding: Entity header is used to compress the media-type.
Content-Type: application/json
Date: The date the response was sent.
Transfer-Encoding: Header specifies the form of encoding used to safely transfer the entity to the user.
Vary: HTTP response header determines how to match future request headers to decide whether a cached response can be used rather than requesting a fresh one from the origin server. We use Accept Encoding
X-Rate-Limit-Requests-Left: Header details how many remaining requests your client can make in the current window before being rate-limited.
X-Rate-Limit-Requests-Quota: Header shows how many API requests are allowed in the current window for your client
X-Rate-Limit-Time-Reset-Ms: Header shows how many milliseconds are remaining in the window.
X-Rate-Limit-Time-Window-Ms: Header shows the size of your current rate-limiting window
+ Body
+ Attributes (ProductResponse)
Edit: The header section is rendering, but now the Body section is just showing the text " + Attributes (ProductPost)"
+ Request
+ Headers
Accept: application/json
Content-Type: application/json
X-Auth-Client: Your Client Id
X-Auth-Token: Your Token
+ Response 200
+ Headers
Content-Encoding: Entity header is used to compress the media-type.
Content-Type: application/json
Date: The date the response was sent.
Transfer-Encoding: Header specifies the form of encoding used to safely transfer the entity to the user.
Vary: HTTP response header determines how to match future request headers to decide whether a cached response can be used rather than requesting a fresh one from the origin server. We use Accept Encoding
X-Rate-Limit-Requests-Left: Header details how many remaining requests your client can make in the current window before being rate-limited.
X-Rate-Limit-Requests-Quota: Header shows how many API requests are allowed in the current window for your client
X-Rate-Limit-Time-Reset-Ms: Header shows how many milliseconds are remaining in the window.
X-Rate-Limit-Time-Window-Ms: Header shows the size of your current rate-limiting window
+ Body
+ Attributes (ProductCollectionResponse)
I am trying to define the Request Body and after reading this:
https://help.apiary.io/api_101/apib-authentication/ &
https://github.com/apiaryio/api-blueprint/blob/master/API%20Blueprint%20Specification.md#def-headers-section
It seemed like I could split them into sections. But the Attributes section is not being recognized. This is a /GET request.
Any ideas why?
+ Request (application/json)
+ Headers
+ Attributes (RequestHeaders)
+ Body
+ Attributes (ProductPost)
Headers section can't contain Attributes, you need to define them explicitly. Just replace:
+ Attributes (RequestHeaders)
with definition of RequestHeaders.
Also try to align Body and Attributes at the same column:
+ Body
+ Attributes (ProductPost)

Data structure with string only

I'm trying to create a Data Structure
# Data Structures
## Incorrect Credentials (string)
- `Insufficient privileges.`
This is where it will be used:
+ Response 401 (application/vnd.api+json)
Not allowed.
+ Attributes (object)
+ errors (array[Incorrect Credentials])
This is what it outputs. I'm trying to replace the "Hello, world!" with "Insufficient privileges.".
Just changing the string to enum should solve it. When inheriting from primitive types you can't set a default value.
# API
# A [GET /]
+ Response 401 (application/vnd.api+json)
Not allowed.
+ Attributes (object)
+ errors (array[Incorrect Credentials])
# Data Structures
## Incorrect Credentials (enum)
- Insufficient privileges.

Jmeter - Set status after few request have been sent

Can I resolve the following scenario by JMeter?:
Send request1
Insert response of request1 to request2
Send request2
Send request3
Compare response3 with responce1
If response3 = responce1 -> setResponseOK() for request2 ELSE Failed
Thanks in advance.
Suggestion to Resolve this situatuion
Send request 1
Capture the response of request 1 in var1(Using correlation-->depending up on your requirement)
Send request 3 first (as you don't need response of request 2 for input of req 3)
Capture response of request 3 in var2
Now hit Request 2 by passing the response of request 1 as an input which is store in var1
Apply beanshell post processor as a child of request 2
Now compare Respose of request 1 and 3 inside if condition:
if matches make the previous request (i.e. Request 2) pass, else fail
if(vars.get("var1").equals(vars.get("var2")))
{
prev.setSuccessful(true);
}
else
{
prev.setSuccessful(false);
}
Design your test as follows:
Request 1
Regular Expression Extractor to extract the whole response into a JMeter Variable configured as:
Reference Name: anything meaningful, i.e. response1
Regular Expression: (?s)(^.*)
Template: $1$
Request 2: use ${response1} where required
Request 3
Response Assertion configured like:
Pattern Matching Rules: Equals
Patterns to Test -> ${response1}
Reference material:
Regular Expressions
How to Use JMeter Assertions in Three Easy Steps

API Blueprint resource definition not working correctly

This blueprint resource is giving me problems.
#Group Data Ingress
## Inbound SMS [/api/v1/inboundSMS{?number,message,key}]
### Receive [GET]
+ Request
+ Parameters
+ number (int) ... The number the sms originated from
+ message (string) ... The message content
+ key (string) ... The inbound authentication key as defined in the application configuration
+ Response 200 (text/plain)
+ Body
OK
As you can see its a simple get request, ie http://my-host.com/api/v1/inboundSMS?number=123&message=hello%20world&key=SECRETKEY
But, I am geting an error from apiary.io
Line: 543 (The request line) Empty request message-body.
Line: 545 (The parameters line) Ignoring unrecognized block.
As it's a GET request there is no message body so I don't know why its complaining about it being missing.
This error is because you have defined a "Request" with no headers or body. The Parameters section belongs outside of the "Request".
You can remove the Request section and add the Parameters section in the outer level as follows to remove this error:
## Inbound SMS [/api/v1/inboundSMS{?number,message,key}]
### Receive [GET]
+ Parameters
+ number (int) ... The number the sms originated from
+ message (string) ... The message content
+ key (string) ... The inbound authentication key as defined in the application configuration
+ Response 200 (text/plain)
+ Body
OKAY
You can also move the Parameters section to the resource instead of placing it in the action. So it will be shared across all actions inside the resource.
## Inbound SMS [/api/v1/inboundSMS{?number,message,key}]
+ Parameters
+ number (int) ... The number the sms originated from
+ message (string) ... The message content
+ key (string) ... The inbound authentication key as defined in the application configuration
### Receive [GET]
+ Response 200 (text/plain)
+ Body
OKAY

Resources