Skip to content

Commit

Permalink
Improve examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mkon committed Oct 5, 2022
1 parent 42c52fb commit ba90486
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 18 deletions.
9 changes: 3 additions & 6 deletions examples/basic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ rackup -q -p 8080

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

| Route | Target |
|----------------|-------------------------------|
| /oauth/ | http://auth.host/oauth/ |
| /oauth/token | http://auth.host/oauth/token |
| /api/ | http://api.host/api/ |
| /api/something | http://api.host/api/something |
| Route | Target |
|----------------|------------------------------------------------|
| /api/* | http://jsonplaceholder.typicode.com/* |
4 changes: 2 additions & 2 deletions examples/basic/config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ app = Rack::Builder.new do
map '/api' do
run ApiValve::Proxy.from_hash(endpoint: 'https://jsonplaceholder.typicode.com')
end
map '/oauth' do
run ApiValve::Proxy.from_hash(endpoint: 'http://auth.host/oauth/')
map '/health' do
run -> (_env) { [200, {}, ['']] }
end
end

Expand Down
14 changes: 7 additions & 7 deletions examples/routing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ rackup -q -p 8080

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

| Method | Route | Target |
|--------|-----------------------|-----------------------|
| GET | /api/* | http://api.host/api/* |
| GET | /api/prefix/* | http://api.host/api/* |
| POST | * | HTTP Error 403 |
| PUT | * | HTTP Error 403 |
| PATCH | * | HTTP Error 403 |
| Method | Route | Target |
|--------|-------------------|---------------------------------------------|
| GET | /api/* | http://jsonplaceholder.typicode.com/* |
| GET | /api/customers/* | http://jsonplaceholder.typicode.com/users/* |
| POST | * | HTTP Error 403 |
| PUT | * | HTTP Error 403 |
| PATCH | * | HTTP Error 403 |
10 changes: 7 additions & 3 deletions examples/routing/config.ru
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
require 'api_valve'
require 'byebug'

app = Rack::Builder.new do
use ApiValve::Middleware::ErrorHandling
use ApiValve::Middleware::Logging

map '/api' do
run ApiValve::Proxy.from_hash(
endpoint: 'http://api.host/api/',
endpoint: 'http://jsonplaceholder.typicode.com',
routes: [
{
method: 'get',
path: %r{^/prefix/(?<final_path>.*)},
request: {path: '%{final_path}'}
path: %r{^/customers/(?<path>.*)},
request: {path: '/users/%{path}'}
},
{
method: 'get'
},
{
method: 'post',
Expand Down
22 changes: 22 additions & 0 deletions spec/examples/routing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
RSpec.describe 'Routing example', type: :feature do
let(:app) { example_app 'routing' }

before do
stub_request(:get, %r{^http://jsonplaceholder.typicode.com/users})
.to_return(status: 204, headers: {'Content-Type' => 'application/json'})
end

describe "GET '/api/customers/1'" do
it 'correctly forwards the request' do
get '/api/customers/1'
expect(WebMock).to(have_requested(:get, 'http://jsonplaceholder.typicode.com/users/1'))
end
end

describe "GET '/api/users/2'" do
it 'correctly forwards the request' do
get '/api/users/2'
expect(WebMock).to(have_requested(:get, 'http://jsonplaceholder.typicode.com/users/2'))
end
end
end

0 comments on commit ba90486

Please sign in to comment.