-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbuilder_spec.rb
More file actions
138 lines (112 loc) · 5.06 KB
/
builder_spec.rb
File metadata and controls
138 lines (112 loc) · 5.06 KB
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
# encoding: UTF-8
# frozen_string_literal: true
require_relative '../spec_helper'
# rubocop:disable Metrics/BlockLength
describe MockServer::Model::DSL do
it 'generates http requests correctly' do
mock_request = http_request(:POST, '/login')
mock_request.query_string_parameters = [parameter('returnUrl', '/account')]
mock_request.cookies = [cookie('sessionId', '2By8LOhBmaW5nZXJwcmludCIlMDAzMW')]
mock_request.body = exact("{username:'foo', password:'bar'}")
expect(to_camelized_hash(HTTP_REQUEST => mock_request)).to eq(FIXTURES.read('post_login_request.json'))
# Block style
mock_request = request(:POST, '/login') do |request|
request.query_string_parameters = [parameter('returnUrl', '/account')]
request.cookies = [cookie('sessionId', '2By8LOhBmaW5nZXJwcmludCIlMDAzMW')]
request.body = exact("{username:'foo', password:'bar'}")
end
expect(to_camelized_hash(HTTP_REQUEST => mock_request)).to eq(FIXTURES.read('post_login_request.json'))
end
it 'generates http responses correctly' do
mock_response = http_response
mock_response.status_code = 401
mock_response.headers = [header('Content-Type', 'application/json; charset=utf-8')]
mock_response.headers << header('Cache-Control', 'public, max-age=86400')
mock_response.body = body('incorrect username and password combination')
mock_response.delay = delay_by(:SECONDS, 1)
expect(to_camelized_hash(HTTP_RESPONSE => mock_response)).to eq(FIXTURES.read('incorrect_login_response.json'))
# Block style
mock_response = response do |response|
response.status_code = 401
response.headers = [header('Content-Type', 'application/json; charset=utf-8')]
response.headers << header('Cache-Control', 'public, max-age=86400')
response.body = body('incorrect username and password combination')
response.delay = delay_by(:SECONDS, 1)
end
expect(to_camelized_hash(HTTP_RESPONSE => mock_response)).to eq(FIXTURES.read('incorrect_login_response.json'))
end
it 'generates http forwards correctly' do
mock_forward = http_forward
mock_forward.host = 'www.mock-server.com'
mock_forward.port = 80
expect(to_camelized_hash(HTTP_FORWARD => mock_forward)).to eq(FIXTURES.read('forward_mockserver.json'))
# Block style
mock_forward = forward do |forward|
forward.host = 'www.mock-server.com'
forward.port = 80
end
expect(to_camelized_hash(HTTP_FORWARD => mock_forward)).to eq(FIXTURES.read('forward_mockserver.json'))
end
it 'generates times correctly' do
expect(to_camelized_hash(HTTP_TIMES => exactly(1))).to eq(FIXTURES.read('times_once.json'))
# Block style
mock_times = times do |times|
times.remaining_times = 1
end
expect(to_camelized_hash(HTTP_TIMES => mock_times)).to eq(FIXTURES.read('times_once.json'))
end
it 'generates expectation correctly from file' do
payload = FIXTURES.read('register_expectation.json')
mock_expectation = expectation_from_json(payload)
expect(to_camelized_hash(mock_expectation.to_hash)).to eq(payload)
end
it 'generates body correctly' do
expect(to_camelized_hash(regex('*/login'))).to eq('type' => 'REGEX', 'value' => '*/login')
expect(to_camelized_hash(xpath('/login[1]'))).to eq('type' => 'XPATH', 'value' => '/login[1]')
expect(to_camelized_hash(parameterized(parameter('token', '4jy5hh')))).to eq('type' => 'PARAMETERS', 'parameters' => [{ 'name' => 'token', 'values' => ['4jy5hh'] }])
end
it 'generates times object correctly' do
expect(to_camelized_hash(unlimited)).to eq('unlimited' => 'true', 'remainingTimes' => 0)
expect(to_camelized_hash(at_least(2))).to eq('unlimited' => 'true', 'remainingTimes' => 2)
end
describe 'transforming request body' do
let(:expectation) { MockServer::Model::Expectation.new }
let(:request) { MockServer::Model::Request.new }
[{ type: :XPATH, value: '/mock/instance' },
{ type: :REGEX, value: '\d+.\d+' },
{ type: :STRING, value: 'Mockserver is great' }].each do |request_hash|
it "generates a request body of type #{request_hash[:type]}" do
request.body = MockServer::Model::Body.new(request_hash)
expectation.request = request
expect(to_camelized_hash(expectation)).to eq(
'httpRequest' => {
'method' => 'GET',
'body' => {
'type' => request_hash[:type].to_s,
'value' => request_hash[:value].to_s
}
}
)
end
end
context 'when request body type binary' do
let(:body) do
MockServer::Model::Body.new(type: :BINARY, value: 'TW9ja3NlcnZlciBpcyBncmVhdAo=')
end
it 'correctly transforms to a string and updates the object' do
request.body = body
expectation.request = request
expect(to_camelized_hash(expectation)).to eq(
'httpRequest' => {
'method' => 'GET',
'body' => {
'type' => 'STRING',
'value' => "Mockserver is great\n"
}
}
)
end
end
end
end
# rubocop:enable Metrics/BlockLength