Skip to content

Commit

Permalink
Support header injection.
Browse files Browse the repository at this point in the history
Bump version
Fix broken js
Add global headers info to README
Exclude tags file from git
Add a note about using this gem with Rails 3.x/4.0
  • Loading branch information
mariochavez committed Jun 10, 2014
1 parent f36998f commit 8c3d0ba
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ spec/dummy/db/*.sqlite3
spec/dummy/log/*.log
spec/dummy/tmp/
spec/dummy/.sass-cache
*.swp
*.un~
tags
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# ApiTaster [![endorse](http://api.coderwall.com/fredwu/endorsecount.png)](http://coderwall.com/fredwu) [![Build Status](https://secure.travis-ci.org/fredwu/api_taster.png?branch=master)](http://travis-ci.org/fredwu/api_taster) [![Dependency Status](https://gemnasium.com/fredwu/api_taster.png)](https://gemnasium.com/fredwu/api_taster)

### NOTE
> If you want to use this gem with Rails 3x/4.0 please specify version 0.7.0 in
your Gemfile.

> Version 0.8 of this gem is compatible only with Rails 4.1.
A quick and easy way to visually test your Rails application's API.

![](http://i.imgur.com/8Dnto.png)
Expand Down Expand Up @@ -69,6 +75,22 @@ ApiTaster.route_path = Rails.root.to_s + "/app/api_tasters" # just an example

If you use a test factory such as [FactoryGirl](https://github.com/thoughtbot/factory_girl), you can require your test factories and share the params. For example in FactoryGirl you can use the `attributes_for(:name_of_factory)` method.

### Custom Headers

If there are certain headers (such as auth token) that need to be present to
consume an API endpoint, you may set then in `APITaster.global_headers` before
`APITaster.routes`:

```ruby
ApiTaster.global_headers = {
'Authorization' => 'Token token=teGpfbVitpnUwm7qStf9'
}

ApiTaster.routes do
# your route definitions
end
```

### Global Params

If there are certain params (such as API version and auth token) that need to be present in every API endpoint, you may set them in `ApiTaster.global_params` before `ApiTaster.routes`:
Expand Down
18 changes: 15 additions & 3 deletions app/assets/javascripts/api_taster/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ var ApiTaster = {

detectContentType: function(response) {
var contentType = response.getResponseHeader("Content-Type");
var detectedContentType = null
var detectedContentType = null;

if (contentType.match(/application\/json/)) {
detectedContentType = 'json';
}
};

return detectedContentType;
},
Expand Down Expand Up @@ -55,7 +55,13 @@ var ApiTaster = {
}

return baseUrl;
}
},

setHeaders: function(headers) {
this.headers = headers;
},

headers: []

};

Expand Down Expand Up @@ -117,6 +123,12 @@ jQuery(function($) {
ApiTaster.disableUrlParams();

window.ajax = $.ajax({
beforeSend: function(xhr) {
var headers = ApiTaster.headers;
for(var l = headers.length, i = 0; i < l; i ++) {
xhr.setRequestHeader(headers[i].key, headers[i].value);
}
},
url: ApiTaster.getSubmitUrl($form),
type: $form.attr('method'),
data: $form.serialize()
Expand Down
4 changes: 4 additions & 0 deletions app/helpers/api_taster/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ def markdown(text)
markdown_renderer ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML)
markdown_renderer.render(text).html_safe
end

def headers_js_callback
render partial: "api_taster/routes/headers.js", locals: {headers: ApiTaster.global_headers}
end
end
end
10 changes: 10 additions & 0 deletions app/views/api_taster/routes/_headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(function() {
if(typeof ApiTaster !== 'undefined') {
ApiTaster.setHeaders(
<%= JSON(headers.collect {|header, value|
{key: header, value: value}
}).html_safe
%>
);
}
}).apply({});
4 changes: 4 additions & 0 deletions app/views/api_taster/routes/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
:label_type => 'important'
%>

<script type="application/javascript">
<%= headers_js_callback %>
</script>

<% if @params.is_a?(Hash) && @params.has_key?(:undefined) %>
<%= render 'undefined_route', :route => @params[:undefined] %>
<% else %>
Expand Down
2 changes: 1 addition & 1 deletion lib/api_taster/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ApiTaster
VERSION = "0.8.0"
VERSION = "0.8.1"
end

0 comments on commit 8c3d0ba

Please sign in to comment.