Sha256: 3585ce15fb6a479f3596c6a4a0c2c9dbdbb04472e5935f594a451d1a2575e1be
Contents?: true
Size: 1.93 KB
Versions: 3
Compression:
Stored size: 1.93 KB
Contents
# frozen_string_literal: true require 'rails-threaded-proxy' require 'json' BACKEND_STUB_PORT = 38_293 RSpec.describe ThreadedProxy::Client do before(:all) do @backend_server = WEBrick::HTTPServer.new(Port: BACKEND_STUB_PORT, Logger: WEBrick::Log.new('/dev/null'), AccessLog: []) @backend_server.mount_proc '/get' do |req, res| raise unless req.request_method == 'GET' res.body = "Received request: #{req.path}" end @backend_server.mount_proc '/post' do |req, res| raise unless req.request_method == 'POST' res.content_type = 'application/json' res.body = JSON.generate(path: req.path, headers: req.header, body: req.body) end @server_thread = Thread.new { @backend_server.start } end after(:all) do @backend_server.shutdown @server_thread.kill end it 'proxies a GET request' do socket = StringIO.new client = ThreadedProxy::Client.new("http://localhost:#{BACKEND_STUB_PORT}/get") client.start(socket) expect(socket.string).to include('Received request: /get') end it 'proxies a POST request with content-length' do socket = StringIO.new client = ThreadedProxy::Client.new("http://localhost:#{BACKEND_STUB_PORT}/post", method: 'post', body: 'hello world') client.start(socket) status, rest = socket.string.split("\r\n", 2) headers, body = rest.split("\r\n\r\n", 2) parsed_body = JSON.parse(body) parsed_headers = headers.split("\r\n").map { |h| h.split(': ', 2) }.to_h expect(status).to eq('HTTP/1.1 200 OK') expect(parsed_headers['content-type']).to eq('application/json') expect(parsed_body['path']).to eq('/post') expect(parsed_body['headers']['content-length']).to eq(['11']) end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
rails-threaded-proxy-0.4.1 | spec/threaded_proxy/client_spec.rb |
rails-threaded-proxy-0.4.0 | spec/threaded_proxy/client_spec.rb |
rails-threaded-proxy-0.3.0 | spec/threaded_proxy/client_spec.rb |