ngw
ngw

Reputation: 1282

http_basic_authentication in request spec

I'm very confused about latest rspec versions. I've found several answers to this same question but being several years old they apparently don't work anymore.

I have a request spec that tests an endpoint that is behind http_basic_authenticate_with. I couldn't find a way to make this work.

My latest attempt is:

      it "returns data_services" do
        request.headers.merge!(authenticated_header(ENV.fetch('HTTP_USERNAME'), ENV.fetch('HTTP_PASSWORD')))
        get data_services_path
        expect(response).to have_http_status(:ok)
      end

Unfortunately there's no request object, which exists only after the get is performed. I've tried to pass the headers to the get method too, but no luck. Is there any way to have requests specs for actions behing http simple auth?

Upvotes: 0

Views: 61

Answers (1)

iftheshoefritz
iftheshoefritz

Reputation: 6129

If you're in a request spec (recommended from Rails v5+), you need to pass your headers to the get method as a keyword argument:

get data_services_path, headers: authenticated_header(...)

The syntax you're trying with request.headers is for controller specs, and the rspec docs recommend switching to request specs and not setting headers in controller specs.

Upvotes: 2

Related Questions