Skip to content

Commit

Permalink
chore: Update dependencies and add JSON parse checking for API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
martdo02 committed Sep 3, 2024
1 parent 97e4072 commit 8c8f863
Show file tree
Hide file tree
Showing 9 changed files with 1,809 additions and 1,023 deletions.
2,712 changes: 1,739 additions & 973 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ln-maf",
"private": true,
"version": "2.0.0",
"version": "2.0.1",
"description": "Modular Automation Framework",
"main": "index.js",
"prepare": "cd packages && node createAutoComplete.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,23 @@ Feature: API - Test the basic items in api
When api request from file "tryB64Post.json" is performed
Then it is written to file "response.txt"

Scenario: quick get Google

Scenario: Get Request - Google
When set "req" to:
"""
{
"url": "https://google.com",
"method": "GET"
}
"""
When perform api request:
"""
${req}
"""
Then status ok
When api request from item "req" is performed

Scenario: Get Request - Google 2
When set "api" to:
"""
{
Expand Down Expand Up @@ -55,7 +71,8 @@ Feature: API - Test the basic items in api
}
"""
Then status not ok
Scenario: quick post Google

Scenario: Post Request - Google
When perform api request:
"""
{
Expand All @@ -66,7 +83,7 @@ Feature: API - Test the basic items in api
}
"""
Then status not ok
Scenario: quick post Google
Scenario: Post Request - Google 2
When perform api request:
"""
{
Expand All @@ -77,21 +94,6 @@ Feature: API - Test the basic items in api
"""
Then status not ok

Scenario: Get Google
When set "req" to:
"""
{
"url": "https://google.com",
"method": "GET"
}
"""
When perform api request:
"""
${req}
"""
Then status ok
When api request from item "req" is performed

Scenario: Get an image using api no attach
Given set "attach" to "false"
Given url "https://cucumber.io"
Expand All @@ -100,13 +102,15 @@ Feature: API - Test the basic items in api
Then status ok
And blob item "response" is written to file "image2.png"
And blob item "response" is attached

Scenario: Get an image using api
Given url "https://cucumber.io"
And api "img/cucumber-school-logo.png"
When method get
Then status ok
And blob item "response" is written to file "image2.png"
And blob item "response" is attached

Scenario: Get an image
When set:
|url|
Expand All @@ -123,30 +127,30 @@ Feature: API - Test the basic items in api
And blob item "response" is written to file "image2.png"
And blob item "response" is attached

Scenario: Get a token
And set "version" to "v3"
When set "request" to:
"""
{
"url": "https://run.mocky.io",
"api": "${version}/d2bc61bc-bdf1-418b-a4d5-dc1b70c86861",
"method": "GET"
}
"""
When perform api request:
"""
${request}
"""
And set "token" to item "response.token"
And set "authorization" to "Auth ${token}"
When api request from file "apiReq.json" is performed
Then status ok
When api request from file "apiReq.json" is performed with:
| version |
| v2 |
Then status not ok
When api request from file "apiReq.json" is performed with:
| version |
| v3 |
| v2 |
Then status not ok
# Scenario: Get a token
# And set "version" to "v3"
# When set "request" to:
# """
# {
# "url": "https://run.mocky.io",
# "api": "${version}/d2bc61bc-bdf1-418b-a4d5-dc1b70c86861",
# "method": "GET"
# }
# """
# When perform api request:
# """
# ${request}
# """
# And set "token" to item "response.token"
# And set "authorization" to "Auth ${token}"
# When api request from file "apiReq.json" is performed
# Then status ok
# When api request from file "apiReq.json" is performed with:
# | version |
# | v2 |
# Then status not ok
# When api request from file "apiReq.json" is performed with:
# | version |
# | v3 |
# | v2 |
# Then status not ok
9 changes: 9 additions & 0 deletions packages/api/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ const b64toBuffer = (b64Data, contentType = '', sliceSize = 512) => {
}

async function performRequestFromJSON (request) {
// fail if request is not an object
if (typeof request !== 'object') {
try {
request = JSON.parse(request)
} catch (err) {
throw new Error(`Error parsing JSON. Check that JSON format is valid for string: ${request}.\n Error: ${err}`)
}
}

if (request.url) { build.call(this, request.url.replace(/\/$/, ''), 'url') }
if (request.body) {
build.call(this, request.body, 'body')
Expand Down
7 changes: 6 additions & 1 deletion packages/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ Given('headers {string}', function (headers) {
})

const performRequestFromJSONString = async function (string) {
const request = JSON.parse(filltemplate(string, this.results))
let request
try {
request = JSON.parse(filltemplate(string, this.results))
} catch (e) {
throw new Error(`Error parsing JSON. Check that JSON format is valid for string: ${string}.\n Error: ${e}`)
}
return await performRequestFromJSONMAF.call(this, request)
}

Expand Down
3 changes: 1 addition & 2 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publishConfig": {
"access": "public"
},
"version": "2.0.6",
"version": "2.0.7",
"description": "",
"main": "index.js",
"scripts": {
Expand All @@ -23,7 +23,6 @@
},
"devDependencies": {
"@cucumber/cucumber": "^9.0.0",
"@ln-maf/core": "^2.0.1",
"@ln-maf/validations": "file:../validations",
"eslint": "^8.18.0",
"eslint-config-standard": "^17.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/api/stepDefinitions/imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('@ln-maf/validations')
2 changes: 2 additions & 0 deletions packages/api/stepDefinitions/steps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('@ln-maf/core/parameter_types')
require('../index.js')
2 changes: 1 addition & 1 deletion packages/postgresql/runFeature.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mkdir -p test/report
if [[ "$ENVIRONMENT" == "COVERAGE" ]]; then
npx nyc --reporter=lcov --reporter=text cucumber-js $EXTRAS -f json:test/report/aws.json --require "stepDefinitions/*.js" features/$*
else
npx cucumber-js $EXTRAS -f json:test/report/aws.json --require "stepDefinitions/*.js" features/$*
npx cucumber-js $EXTRAS -f json:test/report/postgresql.json --require "stepDefinitions/*.js" features/$*
fi
result=$?
npx multiReport
Expand Down

0 comments on commit 8c8f863

Please sign in to comment.