1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
|
require 'spec_helper'
describe JIRA::HttpClient do
let(:basic_client) do
options = JIRA::Client::DEFAULT_OPTIONS
.merge(JIRA::HttpClient::DEFAULT_OPTIONS)
.merge(basic_auth_credentials)
JIRA::HttpClient.new(options)
end
let(:basic_cookie_client) do
options = JIRA::Client::DEFAULT_OPTIONS
.merge(JIRA::HttpClient::DEFAULT_OPTIONS)
.merge(use_cookies: true)
.merge(basic_auth_credentials)
JIRA::HttpClient.new(options)
end
let(:custom_ssl_version_client) do
options = JIRA::Client::DEFAULT_OPTIONS.merge(JIRA::HttpClient::DEFAULT_OPTIONS).merge(ssl_version: :TLSv1_2)
JIRA::HttpClient.new(options)
end
let(:basic_cookie_client_with_context_path) do
options = JIRA::Client::DEFAULT_OPTIONS.merge(JIRA::HttpClient::DEFAULT_OPTIONS).merge(
use_cookies: true,
context_path: '/context'
)
JIRA::HttpClient.new(options)
end
let(:basic_cookie_client_with_additional_cookies) do
options = JIRA::Client::DEFAULT_OPTIONS
.merge(JIRA::HttpClient::DEFAULT_OPTIONS)
.merge(
use_cookies: true,
additional_cookies: ['sessionToken=abc123', 'internal=true']
)
.merge(basic_auth_credentials)
JIRA::HttpClient.new(options)
end
let(:basic_client_cert_client) do
options = JIRA::Client::DEFAULT_OPTIONS.merge(JIRA::HttpClient::DEFAULT_OPTIONS).merge(
use_client_cert: true,
cert: 'public certificate contents',
key: 'private key contents'
)
JIRA::HttpClient.new(options)
end
let(:basic_client_with_no_auth_credentials) do
options = JIRA::Client::DEFAULT_OPTIONS
.merge(JIRA::HttpClient::DEFAULT_OPTIONS)
JIRA::HttpClient.new(options)
end
let(:basic_auth_credentials) do
{ username: 'donaldduck', password: 'supersecret' }
end
let(:proxy_client) do
options = JIRA::Client::DEFAULT_OPTIONS.merge(JIRA::HttpClient::DEFAULT_OPTIONS).merge(
proxy_address: 'proxyAddress',
proxy_port: 42,
proxy_username: 'proxyUsername',
proxy_password: 'proxyPassword'
)
JIRA::HttpClient.new(options)
end
let(:response) do
response = double('response')
allow(response).to receive(:kind_of?).with(Net::HTTPSuccess).and_return(true)
response
end
let(:cookie_response) do
response = double('response')
allow(response).to receive(:kind_of?).with(Net::HTTPSuccess).and_return(true)
response
end
it 'creates an instance of Net:HTTP for a basic auth client' do
expect(basic_client.basic_auth_http_conn.class).to eq(Net::HTTP)
end
it 'makes a correct HTTP request for make_cookie_auth_request' do
request = double
basic_auth_http_conn = double
headers = { 'Content-Type' => 'application/json' }
expected_path = '/context/rest/auth/1/session'
expected_body = '{"username":"","password":""}'
allow(basic_cookie_client_with_context_path).to receive(:basic_auth_http_conn).and_return(basic_auth_http_conn)
expect(basic_auth_http_conn).to receive(:request).with(request).and_return(response)
allow(request).to receive(:basic_auth)
allow(response).to receive(:get_fields).with('set-cookie')
expect(request).to receive(:body=).with(expected_body)
expect(Net::HTTP.const_get(:post.to_s.capitalize)).to receive(:new).with(expected_path, headers).and_return(request)
basic_cookie_client_with_context_path.make_cookie_auth_request
end
it 'responds to the http methods' do
body = ''
headers = double
basic_auth_http_conn = double
request = double
allow(basic_client).to receive(:basic_auth_http_conn).and_return(basic_auth_http_conn)
expect(request).to receive(:basic_auth).with(basic_client.options[:username], basic_client.options[:password]).exactly(5).times.and_return(request)
expect(basic_auth_http_conn).to receive(:request).exactly(5).times.with(request).and_return(response)
%i[delete get head].each do |method|
expect(Net::HTTP.const_get(method.to_s.capitalize)).to receive(:new).with('/path', headers).and_return(request)
expect(basic_client.make_request(method, '/path', nil, headers)).to eq(response)
end
%i[post put].each do |method|
expect(Net::HTTP.const_get(method.to_s.capitalize)).to receive(:new).with('/path', headers).and_return(request)
expect(request).to receive(:body=).with(body).and_return(request)
expect(basic_client.make_request(method, '/path', body, headers)).to eq(response)
end
end
it 'gets and sets cookies' do
body = ''
headers = double
basic_auth_http_conn = double
request = double
allow(basic_cookie_client).to receive(:basic_auth_http_conn).and_return(basic_auth_http_conn)
expect(request).to receive(:basic_auth).with(basic_cookie_client.options[:username], basic_cookie_client.options[:password]).exactly(5).times.and_return(request)
expect(cookie_response).to receive(:get_fields).with('set-cookie').exactly(5).times
expect(basic_auth_http_conn).to receive(:request).exactly(5).times.with(request).and_return(cookie_response)
%i[delete get head].each do |method|
expect(Net::HTTP.const_get(method.to_s.capitalize)).to receive(:new).with('/path', headers).and_return(request)
expect(basic_cookie_client.make_request(method, '/path', nil, headers)).to eq(cookie_response)
end
%i[post put].each do |method|
expect(Net::HTTP.const_get(method.to_s.capitalize)).to receive(:new).with('/path', headers).and_return(request)
expect(request).to receive(:body=).with(body).and_return(request)
expect(basic_cookie_client.make_request(method, '/path', body, headers)).to eq(cookie_response)
end
end
it 'sets additional cookies when they are provided' do
client = basic_cookie_client_with_additional_cookies
body = ''
headers = double
basic_auth_http_conn = double
request = double
allow(client).to receive(:basic_auth_http_conn).and_return(basic_auth_http_conn)
expect(request).to receive(:basic_auth).with(client.options[:username], client.options[:password]).exactly(5).times.and_return(request)
expect(request).to receive(:add_field).with('Cookie', 'sessionToken=abc123; internal=true').exactly(5).times
expect(cookie_response).to receive(:get_fields).with('set-cookie').exactly(5).times
expect(basic_auth_http_conn).to receive(:request).exactly(5).times.with(request).and_return(cookie_response)
%i[delete get head].each do |method|
expect(Net::HTTP.const_get(method.to_s.capitalize)).to receive(:new).with('/path', headers).and_return(request)
expect(client.make_request(method, '/path', nil, headers)).to eq(cookie_response)
end
%i[post put].each do |method|
expect(Net::HTTP.const_get(method.to_s.capitalize)).to receive(:new).with('/path', headers).and_return(request)
expect(request).to receive(:body=).with(body).and_return(request)
expect(client.make_request(method, '/path', body, headers)).to eq(cookie_response)
end
end
it 'performs a basic http client request' do
body = nil
headers = double
basic_auth_http_conn = double
http_request = double
expect(Net::HTTP::Get).to receive(:new).with('/foo', headers).and_return(http_request)
expect(basic_auth_http_conn).to receive(:request).with(http_request).and_return(response)
expect(http_request).to receive(:basic_auth).with(basic_client.options[:username], basic_client.options[:password]).and_return(http_request)
allow(basic_client).to receive(:basic_auth_http_conn).and_return(basic_auth_http_conn)
basic_client.make_request(:get, '/foo', body, headers)
end
it 'performs a basic http client request with a full domain' do
body = nil
headers = double
basic_auth_http_conn = double
http_request = double
expect(Net::HTTP::Get).to receive(:new).with('/foo', headers).and_return(http_request)
expect(basic_auth_http_conn).to receive(:request).with(http_request).and_return(response)
expect(http_request).to receive(:basic_auth).with(basic_client.options[:username], basic_client.options[:password]).and_return(http_request)
allow(basic_client).to receive(:basic_auth_http_conn).and_return(basic_auth_http_conn)
basic_client.make_request(:get, 'http://mydomain.com/foo', body, headers)
end
it 'does not try to use basic auth if the credentials are not set' do
body = nil
headers = double
basic_auth_http_conn = double
http_request = double
expect(Net::HTTP::Get).to receive(:new).with('/foo', headers).and_return(http_request)
expect(basic_auth_http_conn).to receive(:request).with(http_request).and_return(response)
expect(http_request).not_to receive(:basic_auth)
allow(basic_client_with_no_auth_credentials).to receive(:basic_auth_http_conn).and_return(basic_auth_http_conn)
basic_client_with_no_auth_credentials.make_request(:get, '/foo', body, headers)
end
it 'returns a URI' do
uri = URI.parse(basic_client.options[:site])
expect(basic_client.uri).to eq(uri)
end
it 'sets up a http connection with options' do
http_conn = double
uri = double
host = double
port = double
expect(uri).to receive(:host).and_return(host)
expect(uri).to receive(:port).and_return(port)
expect(Net::HTTP).to receive(:new).with(host, port).and_return(http_conn)
expect(http_conn).to receive(:use_ssl=).with(basic_client.options[:use_ssl]).and_return(http_conn)
expect(http_conn).to receive(:verify_mode=).with(basic_client.options[:ssl_verify_mode]).and_return(http_conn)
expect(http_conn).to receive(:read_timeout=).with(basic_client.options[:read_timeout]).and_return(http_conn)
expect(basic_client.http_conn(uri)).to eq(http_conn)
end
it 'sets the SSL version when one is provided' do
http_conn = double
uri = double
host = double
port = double
expect(uri).to receive(:host).and_return(host)
expect(uri).to receive(:port).and_return(port)
expect(Net::HTTP).to receive(:new).with(host, port).and_return(http_conn)
expect(http_conn).to receive(:use_ssl=).with(basic_client.options[:use_ssl]).and_return(http_conn)
expect(http_conn).to receive(:verify_mode=).with(basic_client.options[:ssl_verify_mode]).and_return(http_conn)
expect(http_conn).to receive(:ssl_version=).with(custom_ssl_version_client.options[:ssl_version]).and_return(http_conn)
expect(http_conn).to receive(:read_timeout=).with(basic_client.options[:read_timeout]).and_return(http_conn)
expect(custom_ssl_version_client.http_conn(uri)).to eq(http_conn)
end
it 'sets up a non-proxied http connection by default' do
uri = double
host = double
port = double
expect(uri).to receive(:host).and_return(host)
expect(uri).to receive(:port).and_return(port)
proxy_configuration = basic_client.http_conn(uri).class
expect(proxy_configuration.proxy_address).to be_nil
expect(proxy_configuration.proxy_port).to be_nil
expect(proxy_configuration.proxy_user).to be_nil
expect(proxy_configuration.proxy_pass).to be_nil
end
it 'sets up a proxied http connection when using proxy options' do
uri = double
host = double
port = double
expect(uri).to receive(:host).and_return(host)
expect(uri).to receive(:port).and_return(port)
proxy_configuration = proxy_client.http_conn(uri).class
expect(proxy_configuration.proxy_address).to eq(proxy_client.options[:proxy_address])
expect(proxy_configuration.proxy_port).to eq(proxy_client.options[:proxy_port])
expect(proxy_configuration.proxy_user).to eq(proxy_client.options[:proxy_username])
expect(proxy_configuration.proxy_pass).to eq(proxy_client.options[:proxy_password])
end
it 'can use client certificates' do
http_conn = double
uri = double
host = double
port = double
expect(Net::HTTP).to receive(:new).with(host, port).and_return(http_conn)
expect(uri).to receive(:host).and_return(host)
expect(uri).to receive(:port).and_return(port)
expect(http_conn).to receive(:use_ssl=).with(basic_client.options[:use_ssl])
expect(http_conn).to receive(:verify_mode=).with(basic_client.options[:ssl_verify_mode])
expect(http_conn).to receive(:read_timeout=).with(basic_client.options[:read_timeout])
expect(http_conn).to receive(:cert=).with(basic_client_cert_client.options[:ssl_client_cert])
expect(http_conn).to receive(:key=).with(basic_client_cert_client.options[:ssl_client_key])
expect(basic_client_cert_client.http_conn(uri)).to eq(http_conn)
end
it 'returns a http connection' do
http_conn = double
uri = double
expect(basic_client).to receive(:uri).and_return(uri)
expect(basic_client).to receive(:http_conn).and_return(http_conn)
expect(basic_client.basic_auth_http_conn).to eq(http_conn)
end
describe '#make_multipart_request' do
subject do
basic_client.make_multipart_request(path, data, headers)
end
let(:path) { '/foo' }
let(:data) { {} }
let(:headers) { { 'X-Atlassian-Token' => 'no-check' } }
let(:basic_auth_http_conn) { double }
let(:request) { double('Http Request', path: path) }
let(:response) { double('response') }
before do
allow(request).to receive(:basic_auth)
allow(Net::HTTP::Post::Multipart).to receive(:new).with(path, data, headers).and_return(request)
allow(basic_client).to receive(:basic_auth_http_conn).and_return(basic_auth_http_conn)
allow(basic_auth_http_conn).to receive(:request).with(request).and_return(response)
end
it 'performs a basic http client request' do
expect(request).to receive(:basic_auth).with(basic_client.options[:username], basic_client.options[:password]).and_return(request)
subject
end
it 'makes a correct HTTP request' do
expect(basic_auth_http_conn).to receive(:request).with(request).and_return(response)
expect(response).to receive(:is_a?).with(Net::HTTPOK)
subject
end
end
end
|