|
29 | 29 | Async::HTTP::Endpoint.parse('http://127.0.0.1:9294')
|
30 | 30 | }
|
31 | 31 |
|
32 |
| - it "client can get resource" do |
33 |
| - app = ->(request) do |
34 |
| - Protocol::HTTP::Response[200, {}, ["Hello World"]] |
| 32 | + def run_server(response) |
| 33 | + Async do |task| |
| 34 | + begin |
| 35 | + server_task = task.async do |
| 36 | + app = ->(_) { response } |
| 37 | + Async::HTTP::Server.new(app, endpoint).run |
| 38 | + end |
| 39 | + |
| 40 | + yield |
| 41 | + ensure |
| 42 | + server_task.stop |
| 43 | + end |
| 44 | + end.wait |
| 45 | + end |
| 46 | + |
| 47 | + def get_response(url, path) |
| 48 | + connection = Faraday.new(url: url) do |faraday| |
| 49 | + faraday.response :logger |
| 50 | + faraday.adapter :async_http |
35 | 51 | end
|
36 | 52 |
|
37 |
| - server = Async::HTTP::Server.new(app, endpoint) |
| 53 | + connection.get(path) |
| 54 | + end |
| 55 | + |
| 56 | + it "client can get resource" do |
| 57 | + run_server(Protocol::HTTP::Response[200, {}, ['Hello World']]) do |
| 58 | + response = get_response(endpoint.url, '/index') |
38 | 59 |
|
39 |
| - Async do |task| |
40 |
| - server_task = task.async do |
41 |
| - server.run |
42 |
| - end |
43 |
| - |
44 |
| - connection = Faraday.new(:url => endpoint.url) do |faraday| |
45 |
| - faraday.response :logger |
46 |
| - faraday.adapter :async_http |
47 |
| - end |
48 |
| - |
49 |
| - response = connection.get("/index") |
50 |
| - |
51 |
| - expect(response.body).to be == "Hello World" |
52 |
| - |
53 |
| - server_task.stop |
| 60 | + expect(response.body).to eq 'Hello World' |
54 | 61 | end
|
55 | 62 | end
|
56 | 63 |
|
57 | 64 | it "can get remote resource" do
|
58 |
| - Async do |task| |
59 |
| - connection = Faraday.new(:url => "http://www.google.com") do |faraday| |
60 |
| - faraday.response :logger |
61 |
| - faraday.adapter :async_http |
62 |
| - end |
63 |
| - |
64 |
| - response = connection.get("/search?q=cats") |
| 65 | + Async do |
| 66 | + response = get_response('http://www.google.com', '/search?q=cats') |
65 | 67 |
|
66 | 68 | expect(response).to be_success
|
67 | 69 | end
|
68 | 70 | end
|
| 71 | + |
| 72 | + it 'properly handles chunked responses' do |
| 73 | + large_response_size = 65536 |
| 74 | + |
| 75 | + run_server(Protocol::HTTP::Response[200, {}, ['.' * large_response_size]]) do |
| 76 | + response = get_response(endpoint.url, '/index') |
| 77 | + |
| 78 | + expect(response.body.size).to eq large_response_size |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + it 'properly handles no content responses' do |
| 83 | + run_server(Protocol::HTTP::Response[204]) do |
| 84 | + response = get_response(endpoint.url, '/index') |
| 85 | + |
| 86 | + expect(response.body).to be_nil |
| 87 | + end |
| 88 | + end |
69 | 89 | end
|
0 commit comments