meshblu-server-http
Version:
Maybe. Unofficial. You know how it is.
302 lines (250 loc) • 9.31 kB
text/coffeescript
_ = require 'lodash'
request = require 'request'
Server = require '../../src/server'
async = require 'async'
redis = require 'redis'
RedisNS = require '@octoblu/redis-ns'
JobManager = require 'meshblu-core-job-manager'
describe 'POST /search/devices, GET /v2/devices, GET /devices', ->
beforeEach (done) ->
= 0xd00d
= new Server
port:
disableLogging: true
jobTimeoutSeconds: 1
namespace: 'meshblu:server:http:test'
jobLogQueue: 'meshblu:job-log'
jobLogRedisUri: 'redis://localhost:6379'
meshbluHost: 'localhost'
meshbluPort: 3000
.run done
afterEach (done) ->
.stop => done()
beforeEach ->
= _.bindAll new RedisNS 'meshblu:server:http:test', redis.createClient()
= new JobManager client: , timeoutSeconds: 1
describe '->v3', ->
context 'when the request is successful', ->
beforeEach ->
async.forever (next) =>
.getRequest ['request'], (error, ) =>
next
return unless ?
response =
metadata:
code: 200
responseId: .metadata.responseId
name: 'dinosaur-getter'
data: [
{uuid: 't-rex'}
{uuid: 'megalodon'}
{uuid: 'killasaurus'}
]
.createResponse 'response', response, (error) =>
throw error if error?
beforeEach (done) ->
options =
auth:
username: 'irritable-captian'
password: 'poop-deck'
json: type: 'dinosaur'
headers:
'x-meshblu-as': 'treasure-map'
'x-meshblu-erik-feature': 'custom-headers'
request.post "http://localhost:#{@port}/search/devices", options, (error, , ) =>
done error
it 'should return a 200', ->
expect(.statusCode).to.equal 200
it 'should dispatch the correct metadata', ->
expect().to.containSubset
metadata:
fromUuid: 'treasure-map'
erikFeature: 'custom-headers'
auth:
uuid: 'irritable-captian'
token: 'poop-deck'
it 'should send the search body as the data of the job', ->
data = JSON.parse .rawData
expect(data).to.containSubset type: 'dinosaur'
it 'should have a devices array in the response', ->
expect().to.be.an.array
expect(.length).to.equal 3
describe '->v2', ->
context 'when the request is successful', ->
beforeEach ->
async.forever (next) =>
.getRequest ['request'], (error, ) =>
next
return unless ?
response =
metadata:
code: 200
responseId: .metadata.responseId
data: [
{uuid: 't-rex'}
{uuid: 'megalodon'}
{uuid: 'killasaurus'}
]
.createResponse 'response', response, (error) =>
throw error if error?
beforeEach (done) ->
options =
auth:
username: 'irritable-captian'
password: 'poop-deck'
json: true
qs:
type: 'dinosaur'
headers:
'x-meshblu-as': 'treasure-map'
request.get "http://localhost:#{@port}/v2/devices", options, (error, , ) =>
done error
it 'should return a 200', ->
expect(.statusCode).to.equal 200
it 'should dispatch the correct metadata', ->
expect().to.containSubset
metadata:
fromUuid: 'treasure-map'
auth:
uuid: 'irritable-captian'
token: 'poop-deck'
it 'should send the search body as the data of the job', ->
data = JSON.parse .rawData
expect(data).to.deep.equal type: 'dinosaur'
it 'should have a devices array in the response', ->
expect().to.be.an.array
expect(.length).to.equal 3
describe '->mydevices', ->
context 'when the request is successful', ->
beforeEach ->
async.forever (next) =>
.getRequest ['request'], (error, ) =>
next
return unless ?
response =
metadata:
code: 200
responseId: .metadata.responseId
data: [
{uuid: 't-rex'}
{uuid: 'megalodon'}
{uuid: 'killasaurus'}
]
.createResponse 'response', response, (error) =>
throw error if error?
beforeEach (done) ->
options =
auth:
username: 'irritable-captian'
password: 'poop-deck'
json: true
qs:
type: 'dinosaur'
headers:
'x-meshblu-as': 'treasure-map'
request.get "http://localhost:#{@port}/mydevices", options, (error, , ) =>
done error
it 'should return a 200', ->
expect(.statusCode).to.equal 200
it 'should dispatch the correct metadata', ->
expect().to.containSubset
metadata:
fromUuid: 'treasure-map'
auth:
uuid: 'irritable-captian'
token: 'poop-deck'
it 'should send the search body as the data of the job', ->
data = JSON.parse .rawData
expect(data).to.deep.equal type: 'dinosaur', owner: 'treasure-map'
it 'should have a devices array in the response', ->
expect(.devices).to.be.an.array
expect(.devices.length).to.equal 3
describe '->v1', ->
context 'when the request is successful', ->
beforeEach ->
async.forever (next) =>
.getRequest ['request'], (error, ) =>
next
return unless ?
response =
metadata:
code: 200
responseId: .metadata.responseId
data: [
{uuid: 't-rex'}
{uuid: 'megalodon'}
{uuid: 'killasaurus'}
]
.createResponse 'response', response, (error) =>
throw error if error?
beforeEach (done) ->
options =
auth:
username: 'irritable-captian'
password: 'poop-deck'
json: true
qs:
type: 'dinosaur'
headers:
'x-meshblu-as': 'treasure-map'
request.get "http://localhost:#{@port}/devices", options, (error, , ) =>
done error
it 'should return a 200', ->
expect(.statusCode).to.equal 200
it 'should dispatch the correct metadata', ->
expect().to.containSubset
metadata:
fromUuid: 'treasure-map'
auth:
uuid: 'irritable-captian'
token: 'poop-deck'
it 'should send the search body as the data of the job', ->
data = JSON.parse .rawData
expect(data).to.deep.equal type: 'dinosaur'
it 'should have a devices array in the response', ->
expect(.devices.length).to.equal 3
context 'when the request is successful and a uuid and token is in the query', ->
beforeEach ->
async.forever (next) =>
.getRequest ['request'], (error, ) =>
next
return unless ?
response =
metadata:
code: 200
responseId: .metadata.responseId
data: [
{uuid: 't-rex'}
{uuid: 'megalodon'}
{uuid: 'killasaurus'}
]
.createResponse 'response', response, (error) =>
throw error if error?
beforeEach (done) ->
options =
auth:
username: 'irritable-captian'
password: 'poop-deck'
json: true
qs:
type: 'dinosaur'
uuid: 'use-this-uuid'
token: 'use-this-token'
headers:
'x-meshblu-as': 'treasure-map'
request.get "http://localhost:#{@port}/devices", options, (error, , ) =>
done error
it 'should return a 200', ->
expect(.statusCode).to.equal 200
it 'should dispatch the correct metadata', ->
expect().to.containSubset
metadata:
fromUuid: 'treasure-map'
auth:
uuid: 'use-this-uuid'
token: 'use-this-token'
it 'should send the search body as the data of the job', ->
data = JSON.parse .rawData
expect(data).to.deep.equal uuid: 'use-this-uuid', type: 'dinosaur'
it 'should have a devices array in the response', ->
expect(.devices.length).to.equal 3