-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathrequest_test.rb
73 lines (67 loc) · 3.25 KB
/
request_test.rb
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
require 'test_helper'
require 'uri'
require 'net/http'
require 'mocha'
require 'templates/templates_plugin'
require "proxy/util"
require 'proxy/request'
require 'webmock/test_unit'
class RequestTest < Test::Unit::TestCase
def setup
@foreman_url = 'https://foreman.example.com'
Proxy::SETTINGS.stubs(:foreman_url).returns(@foreman_url)
@template_url = 'http://proxy.lan:8443'
Proxy::Templates::Plugin.load_test_settings(:template_url => @template_url)
@request = Proxy::HttpRequest::ForemanRequest.new
end
def test_get
stub_request(:get, @foreman_url + '/path').to_return(:status => [200, 'OK'], :body => "body")
proxy_req = @request.request_factory.create_get("/path")
result = @request.send_request(proxy_req)
assert_equal("body", result.body)
end
def test_get_with_headers
stub_request(:get, @foreman_url + '/path?a=b').with(:headers => {"h1" => "header"}).to_return(:status => [200, 'OK'], :body => "body")
proxy_req = @request.request_factory.create_get("/path", {"a" => "b"}, "h1" => "header")
result = @request.send_request(proxy_req)
assert_equal("body", result.body)
end
def test_get_with_nested_params
stub_request(:get, @foreman_url + '/register?activation_keys%5B%5D=ac_AlmaLinux8&location_id=2&organization_id=1&repo_data%5B%5D%5Brepo%5D=repo1&repo_data%5B%5D%5Brepo_gpg_key_url%5D=key1&repo_data%5B%5D%5Brepo%5D=repo2&repo_data%5B%5D%5Brepo_gpg_key_url%5D=key2&update_packages=false')
.with(:headers => {"h1" => "header"}).to_return(status: 200, body: "body", headers: {})
request_params =
{ "activation_keys" => ["ac_AlmaLinux8"],
"location_id" => "2",
"organization_id" => "1",
"repo_data" => [
{"repo" => "repo1", "repo_gpg_key_url" => "key1"},
{"repo" => "repo2", "repo_gpg_key_url" => "key2"},
],
"update_packages" => "false" }
proxy_req = @request.request_factory.create_get("/register", request_params, "h1" => "header")
result = @request.send_request(proxy_req)
assert_equal("body", result.body)
end
def test_post
stub_request(:post, @foreman_url + '/path').with(:body => "body").to_return(:status => [200, 'OK'], :body => "body")
proxy_req = @request.request_factory.create_post("/path", "body")
result = @request.send_request(proxy_req)
assert_equal("body", result.body)
end
def test_post_with_nested_params
stub_request(:post, @foreman_url + '/register?activation_keys%5B%5D=ac_AlmaLinux8&location_id=2&organization_id=1&repo_data%5B%5D%5Brepo%5D=repo1&repo_data%5B%5D%5Brepo_gpg_key_url%5D=key1&repo_data%5B%5D%5Brepo%5D=repo2&repo_data%5B%5D%5Brepo_gpg_key_url%5D=key2&update_packages=false')
.to_return(status: 200, body: "body", headers: {h1: "header"})
request_params =
{ "activation_keys" => ["ac_AlmaLinux8"],
"location_id" => "2",
"organization_id" => "1",
"repo_data" => [
{"repo" => "repo1", "repo_gpg_key_url" => "key1"},
{"repo" => "repo2", "repo_gpg_key_url" => "key2"},
],
"update_packages" => "false" }
proxy_req = @request.request_factory.create_post "/register", {"body" => "body"}, {"h1" => "header"}, request_params
result = @request.send_request(proxy_req)
assert_equal("body", result.body)
end
end