Skip to content

Commit

Permalink
feat(httpApi): add definition to replace placeholder in key of state
Browse files Browse the repository at this point in the history
  • Loading branch information
tduyng committed Mar 17, 2022
1 parent 6fcfe13 commit b2b96c2
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 2 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,15 @@ Scenario Outline: Fetching <key> API endpoint from root endpoint
| public_gists_url |
```

You can also pick a field from response headers.

Pick a value from response header

Syntax:
```
I pick response header <key> as <key>
```

Example:

```gherkin
Scenario: Setting json body from .json fixture file
Expand All @@ -240,6 +248,23 @@ Scenario: Setting json body from .json fixture file
And response status code should be 200
```



#### Replace placeholder in key of state:

Syntax:
```
I replace (placeholder) <search> in <key> to <value> with regex option <flags>
```
- `placeholder` and `with regex option <flags>` are optional
- `<value>` does not support spaces

Example:
```
And I replace {token} in URLPage to e1c401d5c
And I replace placeholder {stateUpMode} in URLPage to live with regex option gi
```

#### Using cookies

Cookies are disabled by default, but you've got the ability to enable/disable the feature using a gherkin `Given` expression.
Expand Down Expand Up @@ -827,6 +852,7 @@ Given:
- /^(?:I )?clear request body$/
- /^(?:I )?set request query$/
- /^(?:I )?pick response (json|header) (.+) as (.+)$/
- /^(?:I )?replace(?: placeholder)? (.+) in (.+) to ([^\s]+)(?: with regex options? (.+)?)?$/
- /^(?:I )?enable cookies$/
- /^(?:I )?disable cookies$/
- /^(?:I )?set cookie from (.+)$/
Expand Down
27 changes: 26 additions & 1 deletion doc/README.tpl.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,15 @@ Scenario Outline: Fetching <key> API endpoint from root endpoint
```
<%={{ }}=%>

You can also pick a field from response headers.

Pick a value from response header

Syntax:
```
I pick response header <key> as <key>
```

Example:

{{=<% %>=}}
```gherkin
Expand All @@ -246,6 +254,23 @@ Scenario: Setting json body from .json fixture file
```
<%={{ }}=%>



#### Replace placeholder in key of state:

Syntax:
```
I replace (placeholder) <search> in <key> to <value> with regex option <flags>
```
- `placeholder` and `with regex option <flags>` are optional
- `<value>` does not support spaces

Example:
```
And I replace {token} in URLPage to e1c401d5c
And I replace placeholder {stateUpMode} in URLPage to live with regex option gi
```

#### Using cookies

Cookies are disabled by default, but you've got the ability to enable/disable the feature using a gherkin `Given` expression.
Expand Down
13 changes: 13 additions & 0 deletions src/extensions/http_api/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ exports.install = ({ baseUrl = '' } = {}) => {
this.state.set(key, _.get(data, path))
})

/**
* Pick a value from previous json response or header and set it to state
*/
Given(
/^(?:I )?replace(?: placeholder)? (.+) in (.+) to ([^\s]+)(?: with regex options? (.+)?)?$/,
function (search, key, replaceValue, option) {
let newValue = this.state
.get(key)
.replace(new RegExp(search, option || undefined), replaceValue)
this.state.set(key, newValue)
}
)

/**
* Enabling cookies
*/
Expand Down
40 changes: 40 additions & 0 deletions tests/extensions/http_api/definitions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,46 @@ test('pick response json|header property', () => {
def.shouldMatch('pick response header key as value', ['header', 'key', 'value'])
})

test('replace value of state key', () => {
const context = helper.getContext() // Extension context

const def = context.getDefinitionByMatcher(
'replace(?: placeholder)? (.+) in (.+) to ([^\\s]+)(?: with regex options? (.+)?)?'
)
def.shouldNotMatch('replace key {token} in URLPage to')
def.shouldNotMatch('I replace {token} in URLPage to ')
def.shouldMatch('replace {token} in URLPage to abcd', ['{token}', 'URLPage', 'abcd'])
def.shouldMatch('I replace placeholder {token} in URLPage to abcd', [
'{token}',
'URLPage',
'abcd',
])
def.shouldMatch('I replace placeholder {token} in URLPage to abcd with regex option gi', [
'{token}',
'URLPage',
'abcd',
'gi',
])
def.shouldMatch('I replace {token} in URLPage to abcd with regex options gi', [
'{token}',
'URLPage',
'abcd',
'gi',
])
const URLPage = 'http://localhost:300/api/{token}/page'
const newURLPage = 'http://localhost:300/api/abcd/page'
const stateMock = {
state: {
get: jest.fn().mockImplementation(() => URLPage),
set: jest.fn().mockImplementation(() => newURLPage),
},
}

def.exec(stateMock, '{token}', 'URLPage', 'abcd', 'gi')
expect(stateMock.state.get).toHaveBeenCalledWith('URLPage')
expect(stateMock.state.set).toHaveBeenCalledWith('URLPage', newURLPage)
})

test('enable cookies', () => {
const context = helper.getContext() // Extension context

Expand Down

0 comments on commit b2b96c2

Please sign in to comment.