1

我正在尝试并行运行多个测试,并且我需要为通过 MockServer 发出的每个请求获取独立的响应。

我将 gem https://github.com/jamesdbloom/mockserver-client-ruby用于以下请求:

def mock_successful_transaction_request(successful = true)
    client = initialize_mock
    expectation = expectation do |e|
      e.request do |request|
        request.method = 'POST'
        request.path = '/transactions'
        request.cookies << cookie('sessionId', SecureRandom.hex)
      end

      dummy_response = mock_tb_response(successful)

      e.response do |response|
        response.status_code = 201
        response.body = dummy_response.to_json
      end
    end

    # puts expectation.to_yaml
    client.register(expectation)
 end

def initialize_mock
    client = MockServerClient.new(MOCK_SERVER[:host], MOCK_SERVER[:port])
    client.logger = Logger.new("mockserver_logs.log")
    client
  end

def mock_tb_response(successful)
    tb_id = Faker::Number.between(1, 1_000_000)
    successful ? { id: tb_id, v: '2.0.0' } : {}
  end

当我添加该request.cookies << cookie("sessionId", SecureRandom.hex)部分时,我似乎无法获得响应,并且我的请求失败并显示 HTTP 500。如果我删除它,响应将按预期进行。

我究竟做错了什么?

谢谢你。

4

1 回答 1

0

试试:request.cookies[:sessionId] = cookie('sessionId', SecureRandom.hex)

或者:request.cookies[:sessionId] = SecureRandom.hex

更新

尝试:

request.cookies = [cookie('sessionId', '2By8LOhBmaW5nZXJwcmludCIlMDAzMW')]

如果可行,请尝试用 SecureRandom.hex 替换 '2By8LOhBmaW5nZXJwcmludCIlMDAzMW'

于 2018-03-22T12:07:47.687 回答