Skip to content
This repository has been archived by the owner on Jun 21, 2018. It is now read-only.

Commit

Permalink
Created status code translator
Browse files Browse the repository at this point in the history
  • Loading branch information
stekycz committed Mar 14, 2014
1 parent 767bf1a commit 153b8ea
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion features/MachinesCollection.feature
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Feature: Machines collection

Scenario: Retrieve all Machines
When I do action Machines > Machines collection > Retrieve all Machines
Then It should be Ok (200)
Then It should be Ok
And the response message body is JSON
"""
[{
Expand Down
2 changes: 1 addition & 1 deletion src/content-type-translator.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ contentTypes =

translate = (type) ->
key = type.toLowerCase()
type = contentTypes[key] if contentTypes[key]?
return contentTypes[key] if contentTypes[key]?
return type

module.exports = translate
1 change: 0 additions & 1 deletion src/request/request-processor.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
http = require 'http'
https = require 'https'

class RequestProcessor
constructor: (@request) ->
Expand Down
11 changes: 11 additions & 0 deletions src/status-code-translator.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
http = require 'http'

statusCodes = {}
statusCodes[http.STATUS_CODES[code].toLowerCase()] = code for code in Object.keys(http.STATUS_CODES)

translate = (statusCode) ->
key = statusCode.toLowerCase()
return parseInt statusCodes[key] if statusCodes[key]?
return statusCode

module.exports = translate
6 changes: 4 additions & 2 deletions src/step-definitions.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
World = require './world'
contentTypeTranslate = require './content-type-translator'
statusCodeTranslate = require './status-code-translator'

class ApiBlueprintStepDefinitionsWrapper
constructor: () ->
Expand All @@ -18,13 +19,14 @@ class ApiBlueprintStepDefinitionsWrapper

this.When /^the request message body is(?: (\w+))?$/, (contentType, body, callback) ->
@reset()
@getRequest().setHeader 'content-type', contentTypeTranslate(contentType)
@getRequest().setHeader 'Content-Type', contentTypeTranslate(contentType)
@getRequest().setBody body

callback()

this.Then /It should be ([^()]+)(?: \((\d+)\))?$/, (name, code, callback) ->
@expectedResponse.setStatusCode parseInt(code)
code = statusCodeTranslate name if !code
@expectedResponse.setStatusCode code ? parseInt(code)
@processRequest callback, callback.fail

this.Then /^the response message body is(?: (\w+))?$/, (contentType, body, callback) ->
Expand Down
36 changes: 36 additions & 0 deletions test/unit/status-code-translator-test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'coffee-errors'
{assert} = require 'chai'

translate = require '../../src/status-code-translator'

describe 'status code translation', () ->
describe 'translate ok', () ->
it 'should return 200', () ->
assert.strictEqual translate('ok'), 200

it 'should be case insensitive', () ->
assert.strictEqual translate('OK'), 200

describe 'translate not found', () ->
it 'should return 404', () ->
assert.strictEqual translate('not found'), 404

it 'should be case insensitive', () ->
assert.strictEqual translate('Not Found'), 404

describe 'translate internal server error', () ->
it 'should return 500', () ->
assert.strictEqual translate('internal server error'), 500

it 'should be case insensitive', () ->
assert.strictEqual translate('Internal Server Error'), 500

describe 'translate other', () ->
it 'should return given code', () ->
assert.strictEqual translate('unknown code'), 'unknown code'

it 'should not change letter case', () ->
assert.strictEqual translate('Unknown Code'), 'Unknown Code'

it 'should return every unknown type', () ->
assert.strictEqual translate('quick brown fox jumps over lazy dog'), 'quick brown fox jumps over lazy dog'

0 comments on commit 153b8ea

Please sign in to comment.