Skip to content

Commit

Permalink
feat(collect): add ability to inject previously collected values
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte committed Jun 28, 2017
1 parent 23c490e commit b72de09
Show file tree
Hide file tree
Showing 11 changed files with 343 additions and 92 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ httpApi.install({

``` gherkin
Scenario: Using GitHub API
When I GET https://api.github.com/
Then I should receive a 200 HTTP status code
Given I set User-Agent request header to veggies/1.0
When I GET https://api.github.com/
Then I should receive a 200 HTTP status code
```

[npm-image]: https://img.shields.io/npm/v/@ekino/veggies.svg?style=flat-square
Expand Down
9 changes: 9 additions & 0 deletions examples/features/github.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Feature: GitHub API

Scenario: Using GitHub API
Given I set User-Agent request header to veggies/1.0
When I GET https://api.github.com/
Then I should receive a 200 HTTP status code
When I pick response json emojis_url as emojisUrl
And I GET {{emojisUrl}}
Then I should receive a 200 HTTP status code
14 changes: 14 additions & 0 deletions examples/support/world.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

const { defineSupportCode } = require('cucumber')
const { state, httpApi } = require('../../src')

defineSupportCode(({ setWorldConstructor }) => {
setWorldConstructor(function() {
state.extendWorld(this)
httpApi.extendWorld(this)
})
})

state.install(defineSupportCode)
httpApi.install({})(defineSupportCode)
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"devDependencies": {
"coveralls": "^2.13.1",
"cucumber": "^2.3.1",
"eslint": "^4.1.1",
"jest": "^20.0.4",
"prettier": "^1.5.2"
Expand All @@ -37,6 +38,7 @@
"fmt": "prettier --print-width 140 --tab-width=4 --single-quote --bracket-spacing --no-semi --color --write \"src/**/*.js\"",
"check-fmt": "prettier --print-width 140 --tab-width=4 --single-quote --bracket-spacing --no-semi --list-different \"src/**/*.js\"",
"version": "echo ${npm_package_version}",
"lint": "eslint ."
"lint": "eslint .",
"examples": "cucumberjs --require examples/support ${args} examples/features"
}
}
2 changes: 0 additions & 2 deletions src/extensions/http_api/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ exports.makeRequest = (method, path, baseUrl) => {
reject()
}

console.log(_body) // eslint-disable-line no-console

response = _response
resolve()
})
Expand Down
61 changes: 50 additions & 11 deletions src/extensions/http_api/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,73 @@ const Cast = require('../../cast')
const Helper = require('../../helper')

module.exports = ({ baseUrl = '' }) => ({ Given, When, Then }) => {
Given(/^I set headers to$/, function(step) {
this.httpApiClient.setHeaders(step.rowsHash())
/**
* Setting http headers
*/
Given(/^I set request headers$/, function(step) {
this.httpApiClient.setHeaders(this.state.populateObject(step.rowsHash()))
})

/**
* Setting a single http header
*/
Given(/^I set ([a-zA-Z0-9-]+) request header to (.*)$/, function(key, value) {
this.httpApiClient.setHeader(key, value)
this.httpApiClient.setHeader(key, this.state.populate(value))
})

Given(/^I set request json body to$/, function(step) {
this.httpApiClient.setJsonBody(step.rowsHash())
/**
* Setting json payload
*/
Given(/^I set request json body$/, function(step) {
this.httpApiClient.setJsonBody(this.state.populateObject(step.rowsHash()))
})

Given(/^I set request form body to$/, function(step) {
this.httpApiClient.setFormBody(step.rowsHash())
/**
* Setting form data
*/
Given(/^I set request form body$/, function(step) {
this.httpApiClient.setFormBody(this.state.populateObject(step.rowsHash()))
})

/**
* Setting query parameters
*/
Given(/^I set request query$/, function(step) {
this.httpApiClient.setQuery(this.state.populateObject(step.rowsHash()))
})

Given(/^I set request query to$/, function(step) {
this.httpApiClient.setQuery(step.rowsHash())
Given(/^I pick response json (.*) as (.*)$/, function(path, key) {
const response = this.httpApiClient.getResponse()
const body = response.body

this.state.set(key, _.get(body, path))
})

When(/^I reset http parameters$/, function() {
/**
* Resetting the client's state
*/
When(/^I reset http client/, function() {
this.httpApiClient.reset()
})

/**
* Performing a request
*/
When(/^I (GET|POST|PUT|DELETE) (.*)$/, function(method, path) {
return this.httpApiClient.makeRequest(method, path, baseUrl)
return this.httpApiClient.makeRequest(method, this.state.populate(path), baseUrl)
})

/**
* Dumping response body
*/
When(/^I dump response body$/, function() {
const httpResponse = this.httpApiClient.getResponse()
console.log(httpResponse.body) // eslint-disable-line no-console
})

/**
* Checking response status code
*/
Then(/^I should receive a ([1-5][0-9][0-9]) HTTP status code$/, function(statusCode) {
const httpResponse = this.httpApiClient.getResponse()
expect(httpResponse).to.not.be.empty
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/http_api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const hooks = require('./hooks')

exports.extendWorld = require('./extend_world')

exports.install = ({ baseUrl = '' }) => define => {
exports.install = ({ baseUrl = '' } = {}) => define => {
define(definitions({ baseUrl }))
define(hooks)
}
12 changes: 8 additions & 4 deletions src/extensions/state/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ const _ = require('lodash')

const Cast = require('../../cast')

module.exports = ({ Given }) => {
module.exports = ({ Given, When }) => {
Given(/^I set state key (.*) to (.*)$/, (key, value) => {
this.state[key] = _.template(Cast.value(value))()
this.state.set(key, Cast.value(value))
})

Given(/^I clear state/, function() {
this.state = {}
Given(/^I clear state$/, function() {
this.state.clear()
})

When(/^I dump state$/, function() {
console.log(this.state.dump()) // eslint-disable-line no-console
})
}
3 changes: 2 additions & 1 deletion src/extensions/state/extend_world.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict'

const Helper = require('../../helper')
const state = require('./state')

module.exports = world => {
world.state = {}
world.state = state
Helper.registerExtension(world, 'state')
}
23 changes: 23 additions & 0 deletions src/extensions/state/state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const _ = require('lodash')

let state = {}

exports.set = (key, value) => _.set(state, key, value)

exports.get = key => _.get(state, key)

exports.clear = () => {
state = {}
}

exports.dump = () => state

exports.populate = value => _.template(value, { interpolate: /{{([\s\S]+?)}}/g })(state)

exports.populateObject = object =>
_.mapValues(object, value => {
if (_.isPlainObject(value)) return exports.populateObject(value)
return exports.populate(value)
})

0 comments on commit b72de09

Please sign in to comment.