Skip to content

Commit 0f1e098

Browse files
committed
Improve examples
1 parent 6b58248 commit 0f1e098

File tree

5 files changed

+41
-18
lines changed

5 files changed

+41
-18
lines changed

examples/basic/README.md

+3-6
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ rackup -q -p 8080
88

99
As you can see from the `config.ru`, it will forward all requests according to this table:
1010

11-
| Route | Target |
12-
|----------------|-------------------------------|
13-
| /oauth/ | http://auth.host/oauth/ |
14-
| /oauth/token | http://auth.host/oauth/token |
15-
| /api/ | http://api.host/api/ |
16-
| /api/something | http://api.host/api/something |
11+
| Route | Target |
12+
|----------------|------------------------------------------------|
13+
| /api/* | http://jsonplaceholder.typicode.com/* |

examples/basic/config.ru

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ app = Rack::Builder.new do
44
map '/api' do
55
run ApiValve::Proxy.from_hash(endpoint: 'https://jsonplaceholder.typicode.com')
66
end
7-
map '/oauth' do
8-
run ApiValve::Proxy.from_hash(endpoint: 'http://auth.host/oauth/')
7+
map '/health' do
8+
run ->(_env) { [200, {}, ['']] }
99
end
1010
end
1111

examples/routing/README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ rackup -q -p 8080
88

99
As you can see from the `config.ru`, it will forward all requests according to this table:
1010

11-
| Method | Route | Target |
12-
|--------|-----------------------|-----------------------|
13-
| GET | /api/* | http://api.host/api/* |
14-
| GET | /api/prefix/* | http://api.host/api/* |
15-
| POST | * | HTTP Error 403 |
16-
| PUT | * | HTTP Error 403 |
17-
| PATCH | * | HTTP Error 403 |
11+
| Method | Route | Target |
12+
|--------|-------------------|---------------------------------------------|
13+
| GET | /api/* | http://jsonplaceholder.typicode.com/* |
14+
| GET | /api/customers/* | http://jsonplaceholder.typicode.com/users/* |
15+
| POST | * | HTTP Error 403 |
16+
| PUT | * | HTTP Error 403 |
17+
| PATCH | * | HTTP Error 403 |

examples/routing/config.ru

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
require 'api_valve'
2+
require 'byebug'
23

34
app = Rack::Builder.new do
45
use ApiValve::Middleware::ErrorHandling
56
use ApiValve::Middleware::Logging
67

78
map '/api' do
89
run ApiValve::Proxy.from_hash(
9-
endpoint: 'http://api.host/api/',
10+
endpoint: 'http://jsonplaceholder.typicode.com',
1011
routes: [
1112
{
1213
method: 'get',
13-
path: %r{^/prefix/(?<final_path>.*)},
14-
request: {path: '%{final_path}'}
14+
path: %r{^/customers/(?<path>.*)},
15+
request: {path: '/users/%{path}'}
16+
},
17+
{
18+
method: 'get'
1519
},
1620
{
1721
method: 'post',

spec/examples/routing_spec.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
RSpec.describe 'Routing example', type: :feature do
2+
let(:app) { example_app 'routing' }
3+
4+
before do
5+
stub_request(:get, %r{^http://jsonplaceholder.typicode.com/users})
6+
.to_return(status: 204, headers: {'Content-Type' => 'application/json'})
7+
end
8+
9+
describe "GET '/api/customers/1'" do
10+
it 'correctly forwards the request' do
11+
get '/api/customers/1'
12+
expect(WebMock).to(have_requested(:get, 'http://jsonplaceholder.typicode.com/users/1'))
13+
end
14+
end
15+
16+
describe "GET '/api/users/2'" do
17+
it 'correctly forwards the request' do
18+
get '/api/users/2'
19+
expect(WebMock).to(have_requested(:get, 'http://jsonplaceholder.typicode.com/users/2'))
20+
end
21+
end
22+
end

0 commit comments

Comments
 (0)