Skip to content

Commit

Permalink
[resty.http_ng] globally stub http_ng default backend
Browse files Browse the repository at this point in the history
in unit tests, it should default to test backend
  • Loading branch information
mikz committed Oct 1, 2018
1 parent ee123ca commit fa82104
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
6 changes: 5 additions & 1 deletion gateway/src/resty/http_ng.lua
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,13 @@ local function generate_client_method(client, method_or_format)
return add_http_method(client, method_or_format) or chain_serializer(client, method_or_format)
end

function http.backend()
return resty_backend
end

function http.new(client)
client = client or { }
client.backend = client.backend or resty_backend
client.backend = client.backend or http.backend()

return setmetatable(client, { __index = generate_client_method })
end
Expand Down
10 changes: 5 additions & 5 deletions spec/resty/http_ng_spec.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
local http_ng = require 'resty.http_ng'
local fake_backend = require 'fake_backend_helper'

local resty_backend = require 'resty.http_ng.backend.resty'

describe('http_ng', function()
local http, backend
before_each(function()
backend = fake_backend.new()
http = http_ng.new({backend = backend})
http = http_ng.new{ backend = backend }
end)

for _,method in ipairs{ 'get', 'head', 'options', 'delete' } do
Expand Down Expand Up @@ -210,7 +210,7 @@ describe('http_ng', function()
describe('when there is no error', function()
local response
before_each(function()
http = http_ng.new{}
http = http_ng.new{ backend = resty_backend }
response = http.get('http://127.0.0.1:1984')
end)

Expand All @@ -226,7 +226,7 @@ describe('http_ng', function()
describe('when there is error #network', function()
local response
before_each(function()
http = http_ng.new{}
http = http_ng.new{ backend = resty_backend }
response = http.get('http://127.0.0.1:1')
end)

Expand All @@ -242,7 +242,7 @@ describe('http_ng', function()
describe('works with api.twitter.com #network', function()

it('connects #twitter', function()
local client = http_ng.new{}
local client = http_ng.new{ backend = resty_backend }
local response = client.get('http://api.twitter.com/')
assert(response.ok, 'response is not ok')
end)
Expand Down
18 changes: 18 additions & 0 deletions spec/spec_helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,21 @@ _G.fixture = function (...)

return file.read(path.join('spec', 'fixtures', ...))
end

do -- stub http_ng
local http_ng = require('resty.http_ng')
local test_backend_client = require 'resty.http_ng.backend.test'
local test_backend
local stub = require('luassert.stub')
local stubbed

busted.before_each(function(...)
test_backend = test_backend_client.new()
stubbed = stub(http_ng, 'backend', test_backend)
end)

busted.after_each(function()
test_backend.verify_no_outstanding_expectations()
stubbed:revert()
end)
end

0 comments on commit fa82104

Please sign in to comment.