Use webmock to stub external web requests.

This commit is contained in:
Guo Xiang Tan 2017-05-26 15:19:09 +08:00
parent f8f1548fd4
commit 56f98de7b2
10 changed files with 23 additions and 24 deletions

View File

@ -103,7 +103,7 @@ GEM
railties (>= 3.1)
ember-source (2.10.0)
erubis (2.7.0)
excon (0.53.0)
excon (0.55.0)
execjs (2.7.0)
exifr (1.2.5)
fabrication (2.9.8)
@ -125,7 +125,7 @@ GEM
globalid (0.3.7)
activesupport (>= 4.1.0)
guess_html_encoding (0.0.11)
hashdiff (0.3.2)
hashdiff (0.3.4)
hashie (3.5.5)
highline (1.7.8)
hiredis (0.6.1)

View File

@ -522,10 +522,8 @@ describe DiscourseNarrativeBot::TrackSelector do
describe 'when a quote is requested' do
it 'should create the right reply' do
Excon.stub({ method: :get, hostname: 'api.forismatic.com' },
status: 200,
body: "{\"quoteText\":\"Be Like Water\",\"quoteAuthor\":\"Bruce Lee\"}"
)
stub_request(:get, "http://api.forismatic.com/api/1.0/?format=json&lang=en&method=getQuote").
to_return(status: 200, body: "{\"quoteText\":\"Be Like Water\",\"quoteAuthor\":\"Bruce Lee\"}")
['@discobot quote', 'hello @discobot quote there'].each do |raw|
post.update!(raw: raw)

View File

@ -7,7 +7,7 @@ describe FileHelper do
let(:png) { Base64.decode64("R0lGODlhAQABALMAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwICAgP8AAAD/AP//AAAA//8A/wD//wBiZCH5BAEAAA8ALAAAAAABAAEAAAQC8EUAOw==") }
before do
Excon.stub({ method: :head, hostname: 'eviltrout.com' }, {})
stub_request(:any, /https:\/\/eviltrout.com/)
stub_request(:get, url).to_return(body: png)
end

View File

@ -23,8 +23,10 @@ describe FinalDestination do
end
let(:doc_response) do
{ body: "<html>document</html>",
headers: { "Content-Type" => "text/html" } }
{
status: 200,
headers: { "Content-Type" => "text/html" }
}
end
def redirect_response(from, dest)
@ -64,7 +66,7 @@ describe FinalDestination do
context "underscores in URLs" do
before do
Excon.stub({ method: :head, hostname: 'some_thing.example.com' }, doc_response)
stub_request(:head, 'https://some_thing.example.com').to_return(doc_response)
end
it "doesn't raise errors with underscores in urls" do

View File

@ -74,7 +74,8 @@ describe UploadsController do
controller.stubs(:is_api?).returns(true)
Jobs.expects(:enqueue).with(:create_avatar_thumbnails, anything)
Excon.stub({ method: :head, hostname: 'example.com' }, {})
stub_request(:head, 'http://example.com/image.png')
stub_request(:get, "http://example.com/image.png").to_return(body: File.read('spec/fixtures/images/logo.png'))
xhr :post, :create, url: 'http://example.com/image.png', type: "avatar", synchronous: true

View File

@ -24,7 +24,6 @@ describe UserAvatarsController do
SiteSetting.s3_upload_bucket = "test"
SiteSetting.s3_cdn_url = "http://cdn.com"
Excon.stub({ method: :head, hostname: 'cdn.com' }, {})
stub_request(:get, "http://cdn.com/something/else").to_return(body: 'image')
GlobalSetting.expects(:cdn_url).returns("http://awesome.com/boom")

View File

@ -52,13 +52,9 @@ describe Jobs::EmitWebHookEvent do
end
describe '.web_hook_request' do
before(:all) { Excon.defaults[:mock] = true }
after(:all) { Excon.defaults[:mock] = false }
after(:each) { Excon.stubs.clear }
it 'creates delivery event record' do
Excon.stub({ url: "https://meta.discourse.org/webhook_listener" },
{ body: 'OK', status: 200 })
stub_request(:post, "https://meta.discourse.org/webhook_listener")
.to_return(body: 'OK', status: 200)
expect do
subject.execute(web_hook_id: post_hook.id, event_type: 'post', post_id: post.id)
@ -72,8 +68,8 @@ describe Jobs::EmitWebHookEvent do
end
it 'sets up proper request headers' do
Excon.stub({ url: "https://meta.discourse.org/webhook_listener" },
{ headers: { test: 'string' }, body: 'OK', status: 200 })
stub_request(:post, "https://meta.discourse.org/webhook_listener")
.to_return(headers: { test: 'string' }, body: 'OK', status: 200)
subject.execute(web_hook_id: post_hook.id, event_type: 'ping', event_name: 'ping')
event = WebHookEvent.last
@ -86,7 +82,7 @@ describe Jobs::EmitWebHookEvent do
expect(headers['X-Discourse-Event-Signature']).to eq('sha256=162f107f6b5022353274eb1a7197885cfd35744d8d08e5bcea025d309386b7d6')
expect(event.payload).to eq(MultiJson.dump({ping: 'OK'}))
expect(event.status).to eq(200)
expect(MultiJson.load(event.response_headers)['test']).to eq('string')
expect(MultiJson.load(event.response_headers)['Test']).to eq('string')
expect(event.response_body).to eq('OK')
end
end

View File

@ -6,7 +6,7 @@ describe Jobs::PullHotlinkedImages do
before do
png = Base64.decode64("R0lGODlhAQABALMAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwICAgP8AAAD/AP//AAAA//8A/wD//wBiZCH5BAEAAA8ALAAAAAABAAEAAAQC8EUAOw==")
stub_request(:get, "http://wiki.mozilla.org/images/2/2e/Longcat1.png").to_return(body: png)
Excon.stub({ method: :head, hostname: 'wiki.mozilla.org' }, {})
stub_request(:head, "http://wiki.mozilla.org/images/2/2e/Longcat1.png")
SiteSetting.download_remote_images_to_local = true
FastImage.expects(:size).returns([100, 100]).at_least_once
end

View File

@ -8,7 +8,9 @@ describe Jobs::UpdateGravatar do
expect(user.user_avatar.gravatar_upload_id).to eq(nil)
png = Base64.decode64("R0lGODlhAQABALMAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwICAgP8AAAD/AP//AAAA//8A/wD//wBiZCH5BAEAAA8ALAAAAAABAAEAAAQC8EUAOw==")
stub_request(:get, "http://www.gravatar.com/avatar/d10ca8d11301c2f4993ac2279ce4b930.png?s=360&d=404").to_return(body: png)
url = "http://www.gravatar.com/avatar/d10ca8d11301c2f4993ac2279ce4b930.png?s=360&d=404"
stub_request(:head, url).to_return(status: 200)
stub_request(:get, url).to_return(body: png)
SiteSetting.automatically_download_gravatars = true

View File

@ -86,6 +86,8 @@ Spork.prefork do
require_dependency 'site_settings/local_process_provider'
SiteSetting.provider = SiteSettings::LocalProcessProvider.new
WebMock.disable_net_connect!
end
class DiscourseMockRedis < MockRedis
@ -124,7 +126,6 @@ Spork.prefork do
Discourse.clear_readonly!
I18n.locale = :en
WebMock.reset!
end
class TestCurrentUserProvider < Auth::DefaultCurrentUserProvider