Skip to content
This repository has been archived by the owner on Feb 27, 2022. It is now read-only.

Commit

Permalink
feat(api-response): add api client response
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 29, 2017
1 parent 805be5a commit 893471b
Show file tree
Hide file tree
Showing 10 changed files with 498 additions and 166 deletions.
14 changes: 10 additions & 4 deletions providers/VowProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ class VowProvider extends ServiceProvider {
const Runner = app.use('Test/Runner')
return (title) => {
const suite = Runner.suite(title)
/**
* Returning an object of functions, which are reference to the
* suite class functions.
*
* This is done, since destructuring functions losses the scope
* and `this` resolves to undefined.
*/
return _.transform(Object.getOwnPropertyNames(Object.getPrototypeOf(suite)), (result, prop) => {
result[prop] = function (...args) {
return suite[prop](...args)
Expand All @@ -79,11 +86,10 @@ class VowProvider extends ServiceProvider {
*/
_registerApiClient () {
this.app.bind('Test/ApiClient', (app) => {
const Config = app.use('Adonis/Src/Config')
const ApiClient = require('../src/ApiClient')
return function ({ Context, Request }) {
return function ({ Context, Request, Response }) {
Context.getter('client', function () {
return new ApiClient(Config, Request, this.assert)
return new ApiClient(Request, Response, this.assert)
}, true)
}
})
Expand Down Expand Up @@ -111,11 +117,11 @@ class VowProvider extends ServiceProvider {
* @return {void}
*/
register () {
this._regiterTestSuite()
this._registerTestRunner()
this._registerCli()
this._registerTestCommand()
this._registerApiClient()
this._regiterTestSuite()
}

/**
Expand Down
23 changes: 19 additions & 4 deletions src/ApiClient/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

const superagent = require('superagent')

module.exports = function (BaseRequest) {
module.exports = function (BaseRequest, Response) {
/**
* The api request class is used to make
* HTTP requests.
Expand All @@ -23,7 +23,7 @@ module.exports = function (BaseRequest) {
constructor (url, verb, assert) {
super()
this._agent = superagent[verb](url)
this.assert = assert
this._assert = assert
}

/* istanbul ignore next */
Expand Down Expand Up @@ -255,10 +255,25 @@ module.exports = function (BaseRequest) {
this.headers.forEach((header) => (this._agent.set(header.key, header.value)))
}

const response = await this._agent
let response = null

try {
response = await this._agent
} catch (error) {
response = error.response
}

await this.exec('after')
return response
return new this.constructor.Response(response, this._assert)
}
}

/**
* Reference to response class.
*
* @type {ApiResponse}
*/
ApiRequest.Response = Response

return ApiRequest
}
72 changes: 72 additions & 0 deletions src/ApiClient/Response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict'

/*
* adonis-vow
*
* (c) Harminder Virk <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

module.exports = function (BaseResponse) {
/**
* Api response class is used ot read the http
* response values and run assertions on
* them
*
* @class ApiResponse
* @constructor
*/
class ApiResponse extends BaseResponse {
constructor (res, assert) {
super(assert, res.headers)
this._res = res
}

/**
* Response status
*
* @attribute status
*
* @return {Number}
*/
get status () {
return this._res.status
}

/**
* Response text
*
* @attribute text
*
* @return {String}
*/
get text () {
return this._res.text
}

/**
* Response body
*
* @attribute body
*
* @return {Mixed}
*/
get body () {
return this._res.body
}

/**
* An array of request redirects
*
* @method redirects
*
* @return {Array}
*/
get redirects () {
return this._res.redirects
}
}
return ApiResponse
}
9 changes: 5 additions & 4 deletions src/ApiClient/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ const requestMethods = ['post', 'get', 'patch', 'delete', 'head', 'put']
* @constructor
*/
class ApiClient {
constructor (BaseRequest, assert) {
constructor (BaseRequest, BaseResponse, assert) {
debug('instantiating api client')
this.assert = assert
this.Request = require('./Request')(BaseRequest)
const Response = require('./Response')(BaseResponse)
this.Request = require('./Request')(BaseRequest, Response)
this._assert = assert
}
}

requestMethods.forEach((method) => {
ApiClient.prototype[method] = function (url) {
url = /^http(s)?/.test(url) ? url : `${process.env.TEST_SERVER_URL}${url}`
return new this.Request(url, method, this.assert)
return new this.Request(url, method, this._assert)
}
})

Expand Down
Loading

0 comments on commit 893471b

Please sign in to comment.