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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
# Getting Started
This guide explains how to use the `sus-fixtures-async-http` gem to test HTTP clients and servers.
## Installation
Add the gem to your project:
``` bash
$ bundle add sus-fixtures-async-http
```
## Usage
Here is a simple example showing how to test the default server:
``` ruby
include Sus::Fixtures::Async::HTTP::ServerContext
let(:response) {client.get("/")}
it 'can perform a request' do
expect(response.read).to be == "Hello World!"
end
```
### Custom Applications
You can create a custom application to test your own HTTP handlers:
``` ruby
include Sus::Fixtures::Async::HTTP::ServerContext
let(:app) do
Protocol::HTTP::Middleware.for do |request|
case request.path
when "/"
Protocol::HTTP::Response[200, {}, ["Home Page"]]
when "/api/status"
Protocol::HTTP::Response[200, {"content-type" => "application/json"}, ['{"status":"ok"}']]
else
Protocol::HTTP::Response[404, {}, ["Not Found"]]
end
end
end
it 'serves the home page' do
response = client.get("/")
expect(response.status).to be == 200
expect(response.read).to be == "Home Page"
end
it 'serves API endpoints' do
response = client.get("/api/status")
expect(response.status).to be == 200
expect(response.headers["content-type"]).to be == "application/json"
expect(response.read).to be == '{"status":"ok"}'
end
it 'returns 404 for unknown paths' do
response = client.get("/unknown")
expect(response.status).to be == 404
end
```
### Testing Different HTTP Methods
Test various HTTP methods and request bodies:
``` ruby
include Sus::Fixtures::Async::HTTP::ServerContext
let(:app) do
Protocol::HTTP::Middleware.for do |request|
case [request.method, request.path]
when ["GET", "/users"]
Protocol::HTTP::Response[200, {}, ['[{"id":1,"name":"John"}]']]
when ["POST", "/users"]
body = request.body.read
Protocol::HTTP::Response[201, {}, ["Created: #{body}"]]
when ["PUT", "/users/1"]
body = request.body.read
Protocol::HTTP::Response[200, {}, ["Updated: #{body}"]]
when ["DELETE", "/users/1"]
Protocol::HTTP::Response[204, {}, [""]]
else
Protocol::HTTP::Response[404, {}, ["Not Found"]]
end
end
end
it 'handles GET requests' do
response = client.get("/users")
expect(response.status).to be == 200
end
it 'handles POST requests' do
response = client.post("/users", {}, ['{"name":"Alice"}'])
expect(response.status).to be == 201
expect(response.read).to be(:include?, "Alice")
end
it 'handles PUT requests' do
response = client.put("/users/1", {}, ['{"name":"Bob"}'])
expect(response.status).to be == 200
end
it 'handles DELETE requests' do
response = client.delete("/users/1")
expect(response.status).to be == 204
end
```
## Configuration Options
### Custom URLs and Ports
Override the server URL and endpoint options:
``` ruby
include Sus::Fixtures::Async::HTTP::ServerContext
let(:url) {"http://localhost:9999"}
let(:endpoint_options) do
{reuse_port: true, protocol: protocol, timeout: 30}
end
it 'uses custom configuration' do
expect(bound_url).to be(:include?, ":9999")
end
```
### Protocol Selection
Test with different HTTP protocol versions:
``` ruby
include Sus::Fixtures::Async::HTTP::ServerContext
let(:protocol) {Async::HTTP::Protocol::HTTP2}
it 'uses HTTP/2 protocol' do
response = client.get("/")
expect(response).to have_attributes(version: "HTTP/2.0")
end
```
|