Skip to content

Commit 8e65533

Browse files
committed
Headers added before swaggerUi.load() call.
1 parent ecede5f commit 8e65533

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

app/views/grape_swagger_rails/application/index.html.erb

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
$(function () {
1111
var options = $("html").data('swagger-options');
1212

13+
var headers = {};
14+
<% GrapeSwaggerRails.options.headers.each_with_index do |(key, value), index| %>
15+
<%=raw "headers.header_#{index} = new SwaggerClient.ApiKeyAuthorization('#{CGI.escapeHTML(key)}', '#{CGI.escapeHTML(value)}', 'header');" %>
16+
<% end %>
17+
1318
window.swaggerUi = new SwaggerUi({
1419
url: options.app_url + options.url,
1520
dom_id: "swagger-ui-container",
@@ -50,9 +55,6 @@
5055

5156
window.swaggerUi.load();
5257

53-
<% GrapeSwaggerRails.options.headers.each_with_index do |(key, value), index| %>
54-
<%=raw "window.swaggerUi.api.clientAuthorizations.add('header_#{index}', new SwaggerClient.ApiKeyAuthorization('#{CGI.escapeHTML(key)}', '#{CGI.escapeHTML(value)}', 'header'));" %>
55-
<% end %>
5658
});
5759
</script>
5860
</head>

spec/features/swagger_spec.rb

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
visit '/swagger'
2424
end
2525
it 'adds headers' do
26+
headers = page.evaluate_script("swaggerUi.api.clientAuthorizations")["authz"]
27+
expect(headers.select { |key, value| key.to_s.match(/^header/)}).not_to be_blank
28+
expect(headers.fetch("header_0",{}).fetch('name',{})).to eq GrapeSwaggerRails.options.headers.keys.first
2629
find('#endpointListTogger_headers', visible: true).click
2730
first('a[href="#!/headers/GET_api_headers_format"]', visible: true).click
2831
click_button 'Try it out!'

0 commit comments

Comments
 (0)