This repository was archived by the owner on May 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmock_client_spec.rb
More file actions
82 lines (67 loc) · 3.05 KB
/
mock_client_spec.rb
File metadata and controls
82 lines (67 loc) · 3.05 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
# encoding: UTF-8
# frozen_string_literal: true
require_relative '../spec_helper'
# rubocop:disable Metrics/BlockLength
describe MockServer::MockServerClient do
let(:client) { MockServer::MockServerClient.new('localhost', 8080) }
let(:register_expectation_json) { FIXTURES.read('register_expectation.json').to_json }
let(:search_request_json) { FIXTURES.read('search_request.json').to_json }
before do
# To suppress logging output to standard output, write to a temporary file
client.logger = LoggingFactory::DEFAULT_FACTORY.log('test', output: 'tmp.log', truncate: true)
# Stub requests
stub_request(:put, %r{.+/expectation}).with(body: register_expectation_json, headers: { 'Content-Type' => 'application/json' }).to_return(status: 201)
stub_request(:put, %r{.+/clear}).with(body: search_request_json, headers: { 'Content-Type' => 'application/json' }).to_return(status: 202)
stub_request(:put, %r{.+/reset}).with(headers: { 'Content-Type' => 'application/json' }).to_return(status: 202)
stub_request(:put, %r{.+/retrieve}).with(body: search_request_json, headers: { 'Content-Type' => 'application/json' }).to_return(
body: '[]',
status: 200
)
end
it 'registers an expectation correctly' do
mock_expectation = expectation do |expectation|
expectation.request do |request|
request.method = 'POST'
request.path = '/login'
request.query_string_parameters << parameter('returnUrl', '/account')
request.cookies << cookie('sessionId', '2By8LOhBmaW5nZXJwcmludCIlMDAzMW')
request.body = exact({ username: 'foo', password: 'bar' }.to_json)
end
expectation.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({ message: 'incorrect username and password combination' }.to_json)
response.delay = delay_by(:SECONDS, 1)
end
expectation.times = exactly(2)
end
response = client.register(mock_expectation)
expect(response.code).to eq(201)
end
it 'raises an error when trying to set both forward and response' do
mock_expectation = expectation do |expectation|
expectation.request do |request|
request.method = 'POST'
request.path = '/login'
end
expectation.response do |response|
response.status_code = 401
end
expectation.forward do |forward|
forward.scheme = :HTTP
end
end
expect { client.register(mock_expectation) }.to raise_error(RuntimeError, 'You can only set either of ["httpResponse", "httpForward"]. But not both')
end
it 'clears requests from the mock server' do
expect(client.clear(request(:POST, '/login')).code).to eq(202)
end
it 'resets the mock server' do
expect(client.reset.code).to eq(202)
end
it 'retrieves requests correctly' do
expect(client.retrieve(request(:POST, '/login')).code).to eq(200)
end
end
# rubocop:enable Metrics/BlockLength