intridea/grape — pull request #254 by fabn (2012-10-02 17:35:06+00:00)
Title: RSpec module to provide default headers and default parameters

I've used this in a project which uses Grape (in a Rails project), since request object is not available in specs I've created this module in order to provide headers and parameters for all api calls. Without this the only way to authenticate an user I found is [this](http://qugstart.com/blog/ruby-and-rails/rspec-request-spec-to-test-rails-grape-api-functionality/)

With this instead of having this code

```ruby
describe "some api which requires an header" do
it "should do something with headers" do
get :foo, {}, {'Header-Name' => 'Header value'}
get :bar, {}, {'Header-Name' => 'Header value'}
end
end
```

you can use this simplified code

```ruby
describe "some api which requires an header" do

include RSpec::GrapeHelpers

let(:default_headers) { {'Header-Name' => 'Header value'} }

it "should do something with headers" do
get :foo
get :bar
end
end
```

You can also use http authentication for all requests in this way

```ruby
describe "some http authenticated api" do

include RSpec::GrapeHelpers

let(:http_credentials) { %w(username pass) }

it "should make authenticated api calls" do
get :foo # here the call is built with http user and pass
request.status.should == 200
request.headers.should have_key 'REMOTE_USER'
request.headers['REMOTE_USER'].should = 'username'
end
end
```

Hope this helps