Skip to content

Commit

Permalink
Add all things
Browse files Browse the repository at this point in the history
  • Loading branch information
twinturbo committed May 14, 2012
1 parent 8f18e34 commit a29fb74
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 16 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Expand Up @@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
59 changes: 57 additions & 2 deletions README.md
@@ -1,6 +1,40 @@
# Manifold

TODO: Write a gem description
Mainfold: A generous CORS implementation designed for public APIs.

Use this if you don't care about CORS problems and never want to see
them again.

Don't use this if you have more complex CORS policies.

## Background

CORS is a bitch. It's annoying when you just want to develop your
application. You need to GET/POST/PUT/DELETE to some api in a browser
but it's stopping you. Drop this rack middleware into your stack and
make that it work.

## How It Works

* Handles simple CORS and preflight CORS requests.
* `Access-Control-Accept-Origin: *`.
* Echo's back `Access-Control-Request-Method` for
`Access-Control-Allow-Method` for preflight requests.
* Echo's back `Access-Control-Request-Headers` for
`Access-Control-Allow-Headers` for preflight requests.
* Makes preflight requests cachcable.
* Configurable options for `Access-Control-Expose-Headers`.
* Configurable options for `Access-Control-Accept-Headers`.
* Add `Access-Control-Accept-Origin: *` to simple requests.

## Preflight Requests

CORS preflight requests are sent as OPTIONS requests to whatever URL the
request will made to. Browsers add `Access-Control-Request-Method` and
`Access-Control-Request-Headers`to these requests. The middleware short
circuit these reqeusts to return the CORS response. So be warned: **If
your application accepts `OPTIONS` for routing then you should not use
this code.**

## Installation

Expand All @@ -18,7 +52,28 @@ Or install it yourself as:

## Usage

TODO: Write usage instructions here
```ruby
# config.ru
require 'manifold'

Manifold.expose =+ %w(X-Custom-Header)

use Manifold::Middleware
run MyApp
```

## Rails

Manifold integrates cleanly with Rails. It inserts it's middleware at
the top of the stack and exposes it's configuration through
`Rails.config`. Manifold also add exposes headers added by Rails for
CORS.

```ruby
# application.rb
config.manifold.accept =+ %w(X-Custom-Input-Header) # add custom headers you need
config.manifold.expose =+ %(X-Custom-Output-Header)
```

## Contributing

Expand Down
12 changes: 0 additions & 12 deletions lib/manifold/rails.rb

This file was deleted.

15 changes: 15 additions & 0 deletions lib/manifold/railtie.rb
@@ -0,0 +1,15 @@
module Manifold
class Engine < Rails::Railtie
config.manifold = Manifold.config

initializer "manifold.middleware" do |app|
app.config.middleware.insert 0, Manifold::Middleware
end

initializer "manifold.headers" do |app|
app.config.manifold.expose << "X-Request-Id"
app.config.manifold.expose << "X-Runtime"
app.config.manifold.expose << "X-Rack-Cache"
end
end
end
2 changes: 1 addition & 1 deletion lib/manifold/version.rb
@@ -1,3 +1,3 @@
module Manifold
VERSION = "0.0.1"
VERSION = "1.0.0"
end
1 change: 1 addition & 0 deletions manifold.gemspec
Expand Up @@ -16,5 +16,6 @@ Gem::Specification.new do |gem|
gem.version = Manifold::VERSION

gem.add_development_dependency "rack-test"
gem.add_development_dependency "rails"
gem.add_development_dependency "simplecov"
end
8 changes: 8 additions & 0 deletions test/mainfold_test.rb
Expand Up @@ -4,10 +4,18 @@ class ManifoldTest < MiniTest::Unit::TestCase
include Rack::Test::Methods

def setup
@old_expose = Manifold.config.expose
@old_accept = Manifold.config.accept

Manifold.config.expose = []
Manifold.config.accept = []
end

def teardown
Manifold.config.expose = @old_expose
Manifold.config.accept = @old_accept
end

def app
TestApp
end
Expand Down
16 changes: 16 additions & 0 deletions test/rails_test.rb
@@ -0,0 +1,16 @@
require 'test_helper'

class RailsIntegrationTest < MiniTest::Unit::TestCase
def test_loads_middleware
middleware = TestRailsApp.config.middleware
assert_equal Manifold::Middleware, middleware.first.klass
end

def tests_exposes_headers_uses_in_rails_applications
config = TestRailsApp.config.manifold

assert_includes config.expose, 'X-Request-Id'
assert_includes config.expose, 'X-Runtime'
assert_includes config.expose, 'X-Rack-Cache'
end
end
11 changes: 11 additions & 0 deletions test/test_helper.rb
Expand Up @@ -19,3 +19,14 @@ def self.call(env)
use Manifold::Middleware
run HelloWorld
end

ENV['RAILS_ENV'] = "test"

require 'rails'
require 'action_controller/railtie'
require 'manifold/railtie'

class TestRailsApp < Rails::Application
config.active_support.deprecation = proc { |message, stack| }
initialize!
end

0 comments on commit a29fb74

Please sign in to comment.