Last active
August 29, 2015 14:05
-
-
Save kikito/d10a781b2e7272ad3214 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local spec = require 'spec' | |
local yo = require 'yo-api.yo' | |
describe("yo-api", function() | |
describe("when the uri is /", function() | |
it("sends an email and a notification", function() | |
local request = { method = 'GET', url = '/?username=peter' } | |
local expected_backend_request = { method = 'GET', url = '/?username=peter', headers = {['Content-Type'] = 'application/json'}} | |
local next_middleware = function(req, next_middleware) | |
assert.contains(req, expected_backend_request) -- new assertion | |
return {method = 200, body = 'ok'} | |
end | |
local tester = spec.new(yo) | |
-- this returns the same as before - response, backend_request. We just don't use it on this version | |
tester:run(request, next_middleware) | |
assert.equal(#tester.sent_emails, 1) | |
assert.same(tester.sent_emails, {{to='me@email.com', subject='New Yo subscriber', message='NEW Yo SUBSCRIBER peter'}}) | |
assert.same(tester.sent_events, {{channel='middleware', level='info', msg='new subscriber peter'}}) | |
end) | |
end) | |
describe("then the uri is /yoall/", function() | |
it("passes the apitoken to the backend", function() | |
local request = { method = 'GET', uri = '/yoall/'} | |
local expected_backend_request = { method = 'GET', | |
uri = '/yoall/', | |
headers = {['Content-Type'] = 'application/json'}, | |
body = '{"api_token":"YO_API_TOKEN"}' } | |
local next_middleware = function(req, next_middleware) | |
assert.contains(req, expected_backend_request) -- new assertion | |
return {method = 200, body = 'ok'} | |
end | |
local tester = spec.new(yo) | |
tester:run(request) | |
end) | |
end) | |
describe("when the request is /yo/", function() | |
it("passes & uppercases the username to the backend, plus the API token", function() | |
local request = { method = 'GET', uri = '/yo/', body="username=peter"} | |
local expected_backend_request = { method = 'GET', | |
uri = '/yo/', | |
headers = {['Content-Type'] = 'application/json'}, | |
body = '{"username":"PETER","api_token":"YO_API_TOKEN"}' } | |
local next_middleware = function(req, next_middleware) | |
assert.contains(req, expected_backend_request) -- new assertion | |
return {method = 200, body = 'ok'} | |
end | |
local tester = spec.new(yo) | |
tester:run(request, next_middleware) | |
end) | |
end) | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment