Skip to content

Commit 37cc20d

Browse files
ursmclaude
andcommitted
Initial release
Set default HTTP headers in Rails integration tests. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
0 parents  commit 37cc20d

11 files changed

Lines changed: 271 additions & 0 deletions

File tree

.github/workflows/release.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags: ['v*']
6+
7+
jobs:
8+
release:
9+
runs-on: ubuntu-latest
10+
11+
permissions:
12+
id-token: write
13+
contents: write
14+
15+
steps:
16+
- uses: actions/checkout@v4
17+
18+
- uses: ruby/setup-ruby@v1
19+
with:
20+
ruby-version: '3.4'
21+
22+
- uses: rubygems/release-gem@v1

.github/workflows/test.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
12+
strategy:
13+
fail-fast: false
14+
matrix:
15+
ruby: ['3.3', '3.4']
16+
rails: ['7.1', '7.2', '8.0']
17+
18+
env:
19+
RAILS_VERSION: ${{ matrix.rails }}
20+
21+
steps:
22+
- uses: actions/checkout@v4
23+
24+
- uses: ruby/setup-ruby@v1
25+
with:
26+
ruby-version: ${{ matrix.ruby }}
27+
bundler-cache: true
28+
29+
- run: bundle exec ruby test/minitest/default_http_header_test.rb

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Gemfile.lock

Gemfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
source 'https://rubygems.org'
2+
3+
gemspec
4+
5+
if (rails_version = ENV['RAILS_VERSION'])
6+
gem 'actionpack', "~> #{rails_version}.0"
7+
gem 'activesupport', "~> #{rails_version}.0"
8+
end

LICENSE.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2026 Keita Urashima
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.

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Minitest::DefaultHttpHeader
2+
3+
Set default HTTP headers in Rails integration tests.
4+
5+
Minitest version of [rspec-default_http_header](https://github.com/kenchan/rspec-default_http_header).
6+
7+
## Installation
8+
9+
Add this line to your application's Gemfile:
10+
11+
```ruby
12+
gem 'minitest-default_http_header'
13+
```
14+
15+
## Usage
16+
17+
Use `default_headers` to set HTTP headers that are automatically included in every request:
18+
19+
```ruby
20+
class UsersApiTest < ActionDispatch::IntegrationTest
21+
setup do
22+
user = create(:user)
23+
default_headers['Authorization'] = user.token
24+
end
25+
26+
test 'index' do
27+
get '/api/users.json'
28+
# Authorization header is sent automatically
29+
30+
assert_response :success
31+
end
32+
end
33+
```
34+
35+
Per-request headers are merged with default headers:
36+
37+
```ruby
38+
class UsersApiTest < ActionDispatch::IntegrationTest
39+
setup do
40+
default_headers['Authorization'] = 'your-authorization-token'
41+
end
42+
43+
test 'index as JSON' do
44+
get '/api/users', headers: {'Accept' => 'application/json'}
45+
# Both Authorization and Accept headers are sent
46+
47+
assert_response :success
48+
end
49+
end
50+
```
51+
52+
## Contributing
53+
54+
1. Fork it
55+
2. Create your feature branch (`git checkout -b my-new-feature`)
56+
3. Commit your changes (`git commit -am 'Add some feature'`)
57+
4. Push to the branch (`git push origin my-new-feature`)
58+
5. Create a new Pull Request
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'minitest/default_http_header'
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'minitest/default_http_header/version'
2+
require 'active_support/concern'
3+
require 'active_support/lazy_load_hooks'
4+
5+
module Minitest
6+
module DefaultHttpHeader
7+
extend ActiveSupport::Concern
8+
9+
HTTP_METHODS = %w(get post put patch delete).freeze
10+
11+
def default_headers
12+
@default_headers ||= {}
13+
end
14+
15+
HTTP_METHODS.each do |m|
16+
define_method(m) do |path, *args, **kwargs|
17+
kwargs[:headers] = default_headers.merge(kwargs[:headers] || {})
18+
19+
super(path, *args, **kwargs)
20+
end
21+
end
22+
end
23+
end
24+
25+
ActiveSupport.on_load(:action_dispatch_integration_test) do
26+
include Minitest::DefaultHttpHeader
27+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Minitest
2+
module DefaultHttpHeader
3+
VERSION = '0.1.0'
4+
end
5+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require_relative 'lib/minitest/default_http_header/version'
2+
3+
Gem::Specification.new do |spec|
4+
spec.name = 'minitest-default_http_header'
5+
spec.version = Minitest::DefaultHttpHeader::VERSION
6+
spec.authors = ['Keita Urashima']
7+
spec.email = ['ursm@ursm.jp']
8+
spec.summary = 'Set default HTTP headers in integration tests'
9+
spec.homepage = 'https://github.com/ursm/minitest-default_http_header'
10+
spec.license = 'MIT'
11+
12+
spec.required_ruby_version = '>= 3.3'
13+
14+
spec.files = Dir['lib/**/*.rb', 'LICENSE.txt', 'README.md']
15+
spec.require_paths = ['lib']
16+
17+
spec.add_dependency 'actionpack', '>= 7.1'
18+
spec.add_dependency 'activesupport', '>= 7.1'
19+
end

0 commit comments

Comments
 (0)