├── .gitignore ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── lib └── rspec │ ├── default_http_header.rb │ └── default_http_header │ └── version.rb └── rspec-default_http_header.gemspec /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | *.bundle 11 | *.so 12 | *.o 13 | *.a 14 | mkmf.log 15 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in rspec-default_http_header.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Kenichi TAKAHASHI 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RSpec::DefaultHttpHeader 2 | 3 | Set default http headers in request specs. 4 | 5 | 6 | ## Installation 7 | 8 | Add this line to your application's Gemfile: 9 | 10 | ```ruby 11 | gem 'rspec-default_http_header' 12 | ``` 13 | 14 | And then execute: 15 | 16 | $ bundle 17 | 18 | Or install it yourself as: 19 | 20 | $ gem install rspec-default_http_header 21 | 22 | ## Setup 23 | 24 | Include RSpec::DefaultHeader into your RSpec.configuration. 25 | 26 | ```ruby 27 | RSpec.configure do |config| 28 | config.include RSpec::DefaultHttpHeader, type: :request 29 | end 30 | ``` 31 | 32 | ## Usage 33 | 34 | ### let(:default_headers) 35 | 36 | ```ruby 37 | RSpec.describe 'GET /api/users.json' do 38 | context 'with authorization headers' do 39 | let(:default_headers) { {Authorization: 'your-authorization-token'} } 40 | 41 | before do 42 | get '/api/users.json' 43 | end 44 | 45 | it { expect(response).to be_success} 46 | end 47 | end 48 | ``` 49 | 50 | ### modify default_headers hash 51 | 52 | ```ruby 53 | RSpec.describe 'GET /api/users.json' do 54 | context 'with authorization headers' do 55 | before do 56 | default_headers[:Authorization] = 'your-authorization-token' 57 | 58 | get '/api/users.json' 59 | end 60 | 61 | it { expect(response).to be_success} 62 | end 63 | end 64 | ``` 65 | 66 | ### set in a batch to tests using the tag 67 | 68 | ```ruby 69 | RSpec.configure do |config| 70 | config.before(:example, type: :request, authorized: true) do 71 | default_headers[:Authorization] = 'your-authorization-token' 72 | end 73 | end 74 | 75 | RSpec.describe 'GET /api/users.json' do 76 | context 'with authorization headers', :authorized do 77 | before do 78 | get '/api/users.json' 79 | end 80 | 81 | it { expect(response).to be_success} 82 | end 83 | end 84 | ``` 85 | 86 | ## Contributing 87 | 88 | 1. Fork it ( https://github.com/kenchan/rspec-default_http_header/fork ) 89 | 2. Create your feature branch (`git checkout -b my-new-feature`) 90 | 3. Commit your changes (`git commit -am 'Add some feature'`) 91 | 4. Push to the branch (`git push origin my-new-feature`) 92 | 5. Create a new Pull Request 93 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | 3 | -------------------------------------------------------------------------------- /lib/rspec/default_http_header.rb: -------------------------------------------------------------------------------- 1 | require "rspec/default_http_header/version" 2 | 3 | module RSpec 4 | module DefaultHttpHeader 5 | extend ActiveSupport::Concern 6 | 7 | HTTP_METHODS = %w(get post put delete patch) 8 | 9 | included do 10 | let(:default_headers) { {} } 11 | 12 | HTTP_METHODS.each do |m| 13 | if ActionPack::VERSION::MAJOR >= 5 14 | define_method(m) do |path, *args, **kwargs| 15 | kwargs[:headers] = if kwargs.has_key?(:headers) 16 | default_headers.merge(kwargs[:headers]) 17 | else 18 | default_headers 19 | end 20 | 21 | super(path, *args, **kwargs) 22 | end 23 | else 24 | define_method(m) do |path, parameters = nil, headers_or_env = {}| 25 | super(path, parameters, default_headers.merge(headers_or_env)) 26 | end 27 | end 28 | end 29 | end 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /lib/rspec/default_http_header/version.rb: -------------------------------------------------------------------------------- 1 | module RSpec 2 | module DefaultHttpHeader 3 | VERSION = "0.0.6" 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /rspec-default_http_header.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'rspec/default_http_header/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "rspec-default_http_header" 8 | spec.version = RSpec::DefaultHttpHeader::VERSION 9 | spec.authors = ["Kenichi TAKAHASHI"] 10 | spec.email = ["kenichi.taka@gmail.com"] 11 | spec.summary = %q{Set default http headers in request specs} 12 | spec.description = %q{} 13 | spec.homepage = "http://github.com/kenchan/rspec-default_http_header" 14 | spec.license = "MIT" 15 | 16 | spec.files = `git ls-files -z`.split("\x0") 17 | spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } 18 | spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) 19 | spec.require_paths = ["lib"] 20 | 21 | spec.add_dependency "rspec-rails", "> 3.0" 22 | spec.add_development_dependency "bundler" 23 | spec.add_development_dependency "rake" 24 | end 25 | --------------------------------------------------------------------------------