rest-assured kotlin extension sends an empty body - kotlin

I'm using rest-assured 4.4.0 with Kotlin and this simple test fails:
Given {
body("hello")
}
When {
post("/endpoint/")
} Then {
statusCode(HttpStatus.SC_CREATED)
}
rest-assured is not sending the body with the request:
Request method: POST
Request URI: http://localhost:52298/endpoint/
Proxy: <none>
Request params: <none>
Query params: <none>
Form params: <none>
Path params: <none>
Headers: Accept=*/*
Content-Type=application/json
Cookies: <none>
Multiparts: <none>
Body: <none>
If I switch to the java-based API, the test passes:
given()
.body("hello")
.`when`()
.post("/endpoint")
.then()
.assertThat()
.statusCode(HttpStatus.SC_CREATED)
I couldn't find any open issue in rest-assured github repo related to this problem, I wonder if I'm doing something wrong.

Wrong formatting, the When block must be on the same line as the closing bracket of the Given block.
WRONG
Given {
body("{\"hello\":\"world\"}")
}
When {
...
RIGHT
Given {
body("{\"hello\":\"world\"}")
} When {
...

Related

API Test Automation : How to upload file as the request for a POST Call

`I have a post api which require a file to be uploaded as the request as part of api test automation. we only have to get it uploaded. i have passed it as form param now .
Request method: POST Request URI: Valid url to be passed Proxy: <none> Request params: <none> Query params: <none> Form params: file=Valid file name Path params: <none> Headers: Accept=*/* Content-Type=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=ISO-8859-1 Cookies: <none> Multiparts: <none> Body: <none> I am getting a null value as the response but am expecting a 200 ok with file upload successful message. Any help, appreciated. same is working fine in postman`

Duplicate headers are added at runtime in Rest assured

I am trying to send a request with one header(authorization) but when i check the logs i am seeing same header is added twice to the request which is causing my tests to fail.
Below is the post method which i am using
public static Response doPost(String basePath, Object payload, String header) {
return reqSpec.given().header("X-HSBC-E2E-Trust-Token", header).config(RestAssuredConfig.config())
.body(payload.toString()).log().all()
.post(basePath).then().extract().response();
}
Log:
Request URI: https:Url
Proxy: <none>
Request params: <none>
Query params: <none>
Form params: <none>
Path params: <none>
Headers:E2E-Trust-Token=0JDX09VRIMl9TRVJWRVJfREVWIn0.eyJzaXQiOiJhZDp1c3I6ZW1wbG95Z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iR0ItU1ZDLUZXS1RBUElGUCJ9LHsic2l0IjoibG9naW5JZCIsInN1YiI6IkdCLVNWQy1GV0tUQVBJRlAifV0sImdycCI6WyJDTj1JbmZvZGlyLUFQSUF1dG9GV0sHJvZFN0YW5kYXJkLE9VPUlUSURBUEksT1U9QXBwbGljYXRpb25zLE9VPUdyb3VwcyxEQz1JbmZvRGlyLERDPVByb2QsREM9SFNCQyIsIkNOPUluZm9kaXItQVBJQXV0b0ZXSy1Qcm9kQWRtaW4sT1U9SVRJREFQSSxPVT1BcHBsaWN
3VkLnVrLmhzYmMiLCJpYXQiOjE2NTc1MzE2NTEsImV4cCI6MTY1NzUzMjI1MSwiYXVkIjoiR0ItU1ZDLUFQSUZXREVWQEhCRVUiLCJ1c2VyX25hbWUiOiJHQi1TVkMtRldLVEFQSUZQIn0.HgxR7j7fbl5JRQxTFX0Z7OJLk_11Vc4FPEZ9Ec2xVXmUxeE6M3f8wBe4HOrQDJw_gWm_Kvf_hepsvAu0S0ACRQ3P_4i5yf2z0FXtBoUd3v9AvZcB299PZGJhNZ4HxUEP5SdzjQWOuZgdHAFg6GJdgsYBLTuCXQqy-kdqgPczgx7bgKTBfqrWH2I4qH1ZSE2OlksvGPj0Ia1Ts
Content-Type=application/json; charset=UTF-8
Cookies: <none>
Multiparts: <none>
Body:
{
"name": "TestRole11072733",
"requiresApproval": true,
"signatures": [
1091
]
}
10:27:36.574 [main] DEBUG org.apache.http.impl.conn.BasicClientConnectionManager - Get connection for route {s}->https:URL
10:27:36.574 [main] DEBUG org.apache.http.impl.conn.DefaultClientConnectionOperator - Connecting to Endpoint
10:27:37.001 [main] DEBUG org.apache.http.client.protocol.RequestAddCookies - CookieSpec selected: ignoreCookies
10:27:37.001 [main] DEBUG org.apache.http.client.protocol.RequestAuthCache - Auth cache not set in the context
10:27:37.001 [main] DEBUG org.apache.http.client.protocol.RequestProxyAuthentication - Proxy auth state: UNCHALLENGED
10:27:37.001 [main] DEBUG org.apache.http.impl.client.DefaultHttpClient - Attempt 1 to execute request
10:27:37.001 [main] DEBUG org.apache.http.impl.conn.DefaultClientConnection - Sending request: POST Endpoint

GCP API Gateway: Path parameters are being passed as query params

I'm trying to use GCP API Gateway to create a single endpoint for a couple of my backend services (A,B,C,D), each with their own path structure. I have the Gateway configured for one of the services as follows:
swagger: '2.0'
info:
title: <TITLE>
description: <DESC>
version: 1.0.0
schemes:
- https
produces:
- application/json
paths:
/service_a/match/{id_}:
get:
summary: <SUMMARY>
description: <DESC>
operationId: match_id_
parameters:
- required: true
type: string
name: id_
in: path
- required: true
type: boolean
default: false
name: bool_first
in: query
- required: false
type: boolean
default: false
name: bool_Second
in: query
x-google-backend:
address: <cloud_run_url>/match/{id_}
deadline: 60.0
responses:
'200':
description: Successful Response
'422':
description: Validation
This deploys just fine. But when I hit the endpoint gateway_url/service_a/match/123, it gets routed to cloud_run_url/match/%7Bid_%7D?id_=123 instead of cloud_run_url/match/123.
How can I fix this?
Editing my answer as I misunderstood the issue.
It seems like the { are getting leaked from your configuration as ASCII code, so when you call
x-google-backend:
address: <cloud_run_url>/match/{id_}
deadline: 60.0
it doesn't show the correct ID.
So this should be a leak issue from your yaml file and you can approach this the same way as in this thread about using path params

Serverless function with authorizer arn provided returns 401

I am using serverless.
When I setup one of my functions as the following, which includes authorizer, on the client, I receive 401.
However when I remove it, there are no problems.
provider:
name: aws
runtime: nodejs8.10
region: eu-west-1
environment:
USER_POOL_ARN: "arn:aws:cognito-idp:eu-west-1:974280.....:userpool/eu-west-1........"
functions:
create:
handler: handlers/create.main
events:
- http:
path: create
method: post
cors: true
authorizer:
type: COGNITO_USER_POOLS
name: serviceBAuthFunc
arn: ${self:provider.environment.USER_POOL_ARN}
On the client, I expect a logged in user of the same user pool could get expected response. However it returns 401.
Any help is appreciated. Thanks.
After desperate hours spent, I have come up with the solution.
For anyone who comes across the same issue, here is a solution that worked for me.
Add integration: lambda after cors: true (though the order doesn't matter).
Below is just demonstrating that.
functions:
create:
handler: handlers/create.main
events:
- http:
path: create
method: post
cors: true
integration: lambda // this solves the problem
authorizer:
type: COGNITO_USER_POOLS
name: serviceBAuthFunc
arn: ${self:provider.environment.USER_POOL_ARN}
Send Authorization header with the value of Auth.currentSession().idToken.jwtToken while making the request.
Below is an example for sending headers using API of #aws-amplify/api and Auth of #aws-amplify/auth.
const currentSession = await Auth.currentSession()
await API.post(
'your-endpoint-name',
"/your-endpoint-path/..",
{
headers: {
'Authorization': currentSession.idToken.jwtToken
}
}
)

How should I handle an application/javascript response type in Swagger?

I have a swagger app that works when JSON is returned, but not with JSONP. Success and fail cases are described further down.
It is worth noting that the exact controller code shown below works fine under vanilla node/express - the problem seems to occure when the response goes through the swagger response validator.
The snippet of controller code that handles the response is as follows:
retVal.success = true;
retVal.data = jsondata;
res.set('Content-Type', 'application/javascript');
res.jsonp(200, retVal);
swagger.yaml:
swagger: "2.0"
info:
title: sometitle
version: v1
host: localhost:3000
basePath: /
schemes:
- http
consumes:
- application/json
- application/javascript
produces:
- application/json
- application/javascript
- text/javascript
paths:
/loadallrepos:
x-swagger-router-controller: loadallrepos
get:
summary: summary here...
description: |
description here...
operationId: loadallrepos
parameters:
- name: callback
in: query
description: Passed by .ajax call for cross-domain requests
required: false
type: string
responses:
200:
description: Success
schema:
$ref: "#/definitions/loadallreposResponse"
definitions:
loadallreposResponse:
type: object
required:
- success
- data
properties:
success:
type: boolean
data:
type: object
required:
- nextrefresh
properties:
nextrefresh:
type: integer
If I make a request without the callback parameter, it works:
request: localhost:3000/loadallrepos
response:
{
"success": true,
"data": {
"nextrefresh": 3439704
}
}
If I turn it into a jsonp request though, it fails:
request: localhost:3000/loadallrepos?callback=jQuery111106460774131119251_1439402109118&_1439402109119
response:
{
"message": "Response validation failed: value expected to be an array/object but is not",
"failedValidation": true,
"originalResponse": "typeof jQuery111106460774131119251_1439402109118 === 'function' && jQuery111106460774131119251_1439402109118({
"success": true,
"data": {
"nextrefresh": 3442750
}
});"
}
Again, this works under vanilla node/express. Any help is greatly appreciated!
JSONP APIs return a string of JavaScript. Your Swagger definition says your API returns an object defined at #/definitions/loadallreposResponse which is of type object. What happens is swagger-node/swagger-tools sees a type of object and attempts to use JSON.parse on the response body and fails. To support JSONP with Swagger, you need to make your response schema be of type string to avoid response validation failure.