require 'helper' class TestJsonDriver < Test::Unit::TestCase context 'json driver' do context 'incorrect input' do should 'throw exception if url option is not parsed in' do assert_raise FatalException do Jekyll::Drivers::JsonDriver.new({}) end end should 'throw exception if url option is nil' do assert_raise Jekyll::FatalException do Jekyll::Drivers::JsonDriver.new('url' => nil) end end should 'throw exception if parsed in url is invalid' do assert_raise FatalException do json_driver = Jekyll::Drivers::JsonDriver.new('url' => 'abc.com') end end should 'throw exception if parsed in url is not http' do assert_raise FatalException do json_driver = Jekyll::Drivers::JsonDriver.new('url' => 'ftp://abc.com') end end end context 'incorrect environment' do should 'throw exception if server does not respond' do assert_raise Errno::ECONNREFUSED do Jekyll::Drivers::JsonDriver.new('url' => 'http://127.0.0.1:1').load end end should 'throw exception if server returns non-json content' do assert_raise JSON::ParserError do stub_http_request(:get, 'http://test.com/').to_return(body: 'hello, world') Jekyll::Drivers::JsonDriver.new('url' => 'http://test.com/').load end end end context 'correct input and correct environment' do should 'handle http request correctly' do assert_nothing_raised do stub_http_request(:get, 'http://test.com/').to_return(body: '{"ip": "127.0.0.1"}') data = Jekyll::Drivers::JsonDriver.new('url' => 'http://test.com/').load assert_equal data['ip'], '127.0.0.1' end end should 'handle https request correctly' do assert_nothing_raised do stub_http_request(:get, 'https://test.com/').to_return(body: '{"city": "Nanjing"}') data = Jekyll::Drivers::JsonDriver.new('url' => 'https://test.com/').load assert_equal data['city'], 'Nanjing' end end end end end