|
67 | 67 | it 'should return the schema' do
|
68 | 68 | api = Rapid::API.create('ExampleAPI')
|
69 | 69 | schema = api.schema(host: 'api.example.com', namespace: 'v1')
|
70 |
| - expect(schema['host']).to eq 'api.example.com' |
71 |
| - expect(schema['namespace']).to eq 'v1' |
72 |
| - expect(schema['objects']).to be_a Array |
73 |
| - expect(schema['api']).to eq 'ExampleAPI' |
| 70 | + expect(schema[:host]).to eq 'api.example.com' |
| 71 | + expect(schema[:namespace]).to eq 'v1' |
| 72 | + expect(schema[:objects]).to be_a Array |
| 73 | + expect(schema[:api]).to eq 'ExampleAPI' |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + context '.test_endpoint' do |
| 78 | + it 'returns an error if the endpoint name is incorrect' do |
| 79 | + api = Rapid::API.create('ExampleAPI') |
| 80 | + controller = Rapid::Controller.create('ExampleController') |
| 81 | + expect { api.test_endpoint(controller, :invalid) }.to raise_error(Rapid::StandardError, /invalid endpoint name/i) |
| 82 | + end |
| 83 | + |
| 84 | + it 'executes the endpoint' do |
| 85 | + api = Rapid::API.create('ExampleAPI') |
| 86 | + controller = Rapid::Controller.create('ExampleController') do |
| 87 | + endpoint :info do |
| 88 | + field :name, :string |
| 89 | + action do |
| 90 | + response.add_field :name, 'Peter' |
| 91 | + end |
| 92 | + end |
| 93 | + end |
| 94 | + response = api.test_endpoint(controller, :info) |
| 95 | + expect(response.status).to eq 200 |
| 96 | + expect(response.body[:name]).to eq 'Peter' |
| 97 | + end |
| 98 | + |
| 99 | + it 'executes the endpoint through the authenticator' do |
| 100 | + api = Rapid::API.create('ExampleAPI') do |
| 101 | + authenticator do |
| 102 | + potential_error 'AccessDenied' do |
| 103 | + http_status 403 |
| 104 | + code :access_denied |
| 105 | + end |
| 106 | + action do |
| 107 | + if request.headers['Authorization'] == 'Bearer test' |
| 108 | + request.identity = true |
| 109 | + else |
| 110 | + raise_error 'AccessDenied' |
| 111 | + end |
| 112 | + end |
| 113 | + end |
| 114 | + end |
| 115 | + controller = Rapid::Controller.create('ExampleController') do |
| 116 | + endpoint :info do |
| 117 | + field :name, :string |
| 118 | + action do |
| 119 | + response.add_field :name, 'Peter' |
| 120 | + end |
| 121 | + end |
| 122 | + end |
| 123 | + response = api.test_endpoint(controller, :info) |
| 124 | + expect(response.status).to eq 403 |
| 125 | + expect(response.body[:error][:code]).to eq :access_denied |
74 | 126 | end
|
75 | 127 | end
|
76 | 128 | end
|
0 commit comments