Skip to content

Commit 33afc46

Browse files
author
Dieter Späth
committed
added a spec and documentation for default status codes
1 parent 52b63db commit 33afc46

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
============
33

44
* [#687](https://github.com/intridea/grape/pull/687): Fix: `mutually_exclusive` and `exactly_one_of` validation error messages now label parameters as strings, consistently with `requires` and `optional` - [@dblock](https://github.com/dblock).
5-
* [#698](https://github.com/intridea/grape/pull/698): `error!` sets `status` for `Endpoint` to [@dspaeth-faber](https://github.com/dspaeth-faber).
5+
* [#698](https://github.com/intridea/grape/pull/698): `error!` sets `status` for `Endpoint` too - [@dspaeth-faber](https://github.com/dspaeth-faber).
66
* Your contribution here.
77

88
0.8.0 (7/10/2014)

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
- [Helpers](#helpers)
3232
- [Parameter Documentation](#parameter-documentation)
3333
- [Cookies](#cookies)
34+
- [HTTP Status Code](#http-status-code)
3435
- [Redirecting](#redirecting)
3536
- [Allowed Methods](#allowed-methods)
3637
- [Raising Exceptions](#raising-exceptions)
@@ -301,6 +302,21 @@ When an invalid `Accept` header is supplied, a `406 Not Acceptable` error is ret
301302
option is set to `false`. Otherwise a `404 Not Found` error is returned by Rack if no other route
302303
matches.
303304

305+
### HTTP Status Code
306+
307+
By default Grape returns a 200 status code for `GET`-Requests and 201 for `POST`-Requests.
308+
You can use `status` to query and set the actual HTTP Status Code
309+
310+
```ruby
311+
post do
312+
status 202
313+
314+
if status == 200
315+
# do some thing
316+
end
317+
end
318+
```
319+
304320
### Accept-Version Header
305321

306322
```ruby

spec/grape/endpoint_spec.rb

+27
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,33 @@ def app
6767
expect(last_response.status).to eq(206)
6868
expect(last_response.body).to eq("Hello")
6969
end
70+
71+
it 'is set as default to 200 for get' do
72+
memoized_status = nil
73+
subject.get('/home') do
74+
memoized_status = status
75+
"Hello"
76+
end
77+
78+
get '/home'
79+
expect(last_response.status).to eq(200)
80+
expect(memoized_status).to eq(200)
81+
expect(last_response.body).to eq("Hello")
82+
end
83+
84+
it 'is set as default to 201 for post' do
85+
memoized_status = nil
86+
subject.post('/home') do
87+
memoized_status = status
88+
"Hello"
89+
end
90+
91+
post '/home'
92+
expect(last_response.status).to eq(201)
93+
expect(memoized_status).to eq(201)
94+
expect(last_response.body).to eq("Hello")
95+
end
96+
7097
end
7198

7299
describe '#header' do

0 commit comments

Comments
 (0)