Commit f6e74e25 authored by Paul Slaughter's avatar Paul Slaughter Committed by Mayra Cabrera

Add x-gitlab-feature-category header to startup gql calls

parent b74d155c
......@@ -5,6 +5,13 @@ module StartupjsHelper
@graphql_startup_calls
end
def page_startup_graphql_headers
{
'X-CSRF-Token' => form_authenticity_token,
'x-gitlab-feature-category' => ::Gitlab::ApplicationContext.current_context_attribute(:feature_category).presence || ''
}
end
def add_page_startup_graphql_call(query, variables = {})
@graphql_startup_calls ||= []
file_location = File.join(Rails.root, "app/graphql/queries/#{query}.query.graphql")
......
......@@ -17,11 +17,14 @@
});
}
if (gl.startup_graphql_calls && window.fetch) {
const headers = #{page_startup_graphql_headers.to_json};
const url = `#{api_graphql_url}`
const opts = {
method: "POST",
headers: { "Content-Type": "application/json", 'X-CSRF-Token': "#{form_authenticity_token}" },
headers: {
"Content-Type": "application/json",
...headers,
};
gl.startup_graphql_calls = gl.startup_graphql_calls.map(call => ({
......
......@@ -3,6 +3,8 @@
require 'spec_helper'
RSpec.describe StartupjsHelper do
using RSpec::Parameterized::TableSyntax
describe '#page_startup_graphql_calls' do
let(:query_location) { 'repository/path_last_commit' }
let(:query_content) do
......@@ -17,4 +19,24 @@ RSpec.describe StartupjsHelper do
expect(startup_graphql_calls).to include({ query: query_content, variables: { ref: 'foo' } })
end
end
describe '#page_startup_graphql_headers' do
where(:csrf_token, :feature_category, :expected) do
'abc' | 'web_ide' | { 'X-CSRF-Token' => 'abc', 'x-gitlab-feature-category' => 'web_ide' }
'' | '' | { 'X-CSRF-Token' => '', 'x-gitlab-feature-category' => '' }
'abc' | nil | { 'X-CSRF-Token' => 'abc', 'x-gitlab-feature-category' => '' }
'something' | ' ' | { 'X-CSRF-Token' => 'something', 'x-gitlab-feature-category' => '' }
end
with_them do
before do
allow(helper).to receive(:form_authenticity_token).and_return(csrf_token)
::Gitlab::ApplicationContext.push(feature_category: feature_category)
end
it 'returns hash of headers for GraphQL requests' do
expect(helper.page_startup_graphql_headers).to eq(expected)
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment