Skip to content

Commit 71d1c92

Browse files
authored
Fix 400 error on Lighthouse access_token requests (#25703)
The net-http gem v0.7.0 removed automatic Content-Type header defaults for POST requests. This caused access_token requests to the Lighthouse OAuth endpoint to fail with 400 Bad Request because the server couldn't parse the form-encoded body without the Content-Type header. This fix explicitly sets Content-Type: application/x-www-form-urlencoded on the access_token_connection to ensure compatibility with net-http v0.7.0+ and proper OAuth token requests. Root cause: googleauth gem update (298d685) pulled in net-http 0.9.1 which includes the breaking change from v0.7.0. Adds spec to prevent regression.
1 parent eebe9ed commit 71d1c92

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

modules/mobile/app/services/mobile/v0/lighthouse_health/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def api_url
3636
# @return Faraday::Connection a Faraday connection instance with the correct middleware
3737
#
3838
def access_token_connection
39-
Faraday.new(access_token_url) do |conn|
39+
Faraday.new(access_token_url, headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }) do |conn|
4040
conn.use(:breakers, service_name:)
4141
conn.use Faraday::Response::RaiseError
4242
conn.response :json, content_type: /\bjson$/
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
describe Mobile::V0::LighthouseHealth::Configuration do
6+
subject(:config) { described_class.instance }
7+
8+
describe '#access_token_connection' do
9+
it 'sets Content-Type header to application/x-www-form-urlencoded' do
10+
# This header is required for OAuth token requests.
11+
# net-http v0.7.0+ removed automatic Content-Type defaults for POST requests,
12+
# so we must explicitly set this header to prevent 400 errors from the OAuth server.
13+
connection = config.access_token_connection
14+
15+
expect(connection.headers['Content-Type']).to eq('application/x-www-form-urlencoded')
16+
end
17+
18+
it 'includes RaiseError middleware for error handling' do
19+
connection = config.access_token_connection
20+
21+
expect(connection.builder.handlers).to include(Faraday::Response::RaiseError)
22+
end
23+
24+
it 'includes breakers middleware for circuit breaking' do
25+
connection = config.access_token_connection
26+
27+
# Breakers middleware is registered as a symbol
28+
handler_names = connection.builder.handlers.map do |h|
29+
h.name
30+
rescue
31+
h.to_s
32+
end
33+
expect(handler_names.join).to include('breakers').or include('Breakers')
34+
end
35+
36+
it 'includes JSON response middleware' do
37+
connection = config.access_token_connection
38+
39+
expect(connection.builder.handlers).to include(Faraday::Response::Json)
40+
end
41+
end
42+
43+
describe '#connection' do
44+
it 'does not require Content-Type header (used for GET requests with Bearer token)' do
45+
connection = config.connection
46+
47+
# The main connection is used for API requests with Authorization header,
48+
# not for token requests, so Content-Type is not required
49+
expect(connection.headers['Content-Type']).to be_nil
50+
end
51+
end
52+
53+
describe '#service_name' do
54+
it 'returns the correct service name for breakers' do
55+
expect(config.service_name).to eq('MobileLighthouseHealth')
56+
end
57+
end
58+
end

0 commit comments

Comments
 (0)