Skip to content

Instantly share code, notes, and snippets.

@Navaneethsen
Created November 17, 2015 08:33
Show Gist options
  • Select an option

  • Save Navaneethsen/3b12b6523766ddee3187 to your computer and use it in GitHub Desktop.

Select an option

Save Navaneethsen/3b12b6523766ddee3187 to your computer and use it in GitHub Desktop.
require "mockserver-client"
class MockServerTest
include MockServer
include MockServer::Model::DSL
@client = nil
def initialize
@client = MockServerClient.new('localhost', 1080)
end
def createPostExpectation
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=20')
response.body = body({ message: 'incorrect username and password combination' }.to_json)
response.delay = delay_by(:SECONDS, 1)
end
end
expectation.times = exactly(100000)
@client.register(expectation)
end
def createGetExpectation
expectation = expectation do |expectation|
expectation.request do |request|
request.method = 'GET'
request.path = '/assets'
request.query_string_parameters << parameter('num', '12')
end
expectation.response do |response|
response.status_code = 212
response.headers << header('Content-Type', 'application/json; charset=utf-8')
response.headers << header('Cache-Control', 'public, max-age=20')
response.body = body({ message: 'Good Result set' }.to_json)
response.delay = delay_by(:SECONDS, 1)
end
end
expectation.times = exactly(100000)
@client.register(expectation)
end
def createGetExpectationWithCookies
expectation = expectation do |expectation|
expectation.request do |request|
request.method = 'GET'
request.path = '/assets'
request.query_string_parameters << parameter('num', '12')
request.query_string_parameters << parameter('lang', 'eng')
request.cookies = [cookie('sessionId', '2By8LOhBmaW5nZXJwcmludCIlMDAzMW')]
end
expectation.response do |response|
response.status_code = 214
response.headers << header('Content-Type', 'application/json; charset=utf-8')
response.headers << header('Cache-Control', 'public, max-age=20')
response.body = body({ message: 'Good Result set with the given cookies' }.to_json)
response.delay = delay_by(:SECONDS, 1)
end
end
expectation.times = exactly(100000)
@client.register(expectation)
end
end
mockserverTest = MockServerTest.new
mockserverTest.createPostExpectation
mockserverTest.createGetExpectation
mockserverTest.createGetExpectationWithCookies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment