Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set up RSpec-based test suite and seed with tests #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: CI
on:
push:
pull_request:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
build:
strategy:
matrix:
os: [ubuntu-latest]
ruby: ['2.7', '3.0', '3.1']
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Run tests
run: bundle exec rspec
Binary file added spec/fixtures/square.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions spec/fixtures/square.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions spec/open_uri_cache_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require_relative 'spec_helper'

describe 'OpenURI::Cache' do
subject { OpenURI::Cache }

it 'should set cache_path to folder in /tmp scoped to uid' do
expect(subject.cache_path).to eql %(/tmp/open-uri-#{Process.uid})
end

it 'should allow cache_path to be changed' do
old_cache_path = subject.cache_path
subject.cache_path = '/tmp/open-uri'
expect(subject.cache_path).to eql '/tmp/open-uri'
ensure
subject.cache_path = old_cache_path
end
end
47 changes: 47 additions & 0 deletions spec/open_uri_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require_relative 'spec_helper'

describe 'OpenURI.open_uri' do
before do
OpenURI::Cache.cache_path = cache_dir
FileUtils.rm_r OpenURI::Cache.cache_path, force: true, secure: true
end

it 'should cache calls to OpenURI.open_uri for binary file' do
actual = with_local_webserver do |base_url, thr|
url = %(#{base_url}/square.png)
expect(OpenURI::Cache.get url).to be_nil
contents = OpenURI.open_uri(url) {|fd| fd.read }
expect(OpenURI.open_uri(url) {|fd| fd.read }).to eql contents
expect(thr[:requests].length).to eql 1
contents
end
expected = File.read(fixture_file('square.png'), mode: 'rb')
expect(actual).to eql expected
end

it 'should cache calls to OpenURI.open_uri for text file' do
actual = with_local_webserver do |base_url, thr|
url = %(#{base_url}/square.svg)
expect(OpenURI::Cache.get url).to be_nil
contents = OpenURI.open_uri(url) {|fd| fd.read }
expect(OpenURI.open_uri(url) {|fd| fd.read }).to eql contents
expect(thr[:requests].length).to eql 1
contents
end
expected = File.read(fixture_file('square.svg'), mode: 'rb')
expect(actual).to eql expected
end

it 'should allow cached URL to be invalidated immediately' do
actual = with_local_webserver do |base_url, thr|
url = %(#{base_url}/square.png)
contents = OpenURI.open_uri(url) {|fd| fd.read }
OpenURI::Cache.invalidate url
expect(OpenURI.open_uri(url) {|fd| fd.read }).to eql contents
expect(thr[:requests].length).to eql 2
contents
end
expected = File.read(fixture_file('square.png'), mode: 'rb')
expect(actual).to eql expected
end
end
74 changes: 71 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,88 @@
# about re-initializing constants
$VERBOSE = nil

require 'open-uri/cached'
require 'fileutils' unless defined? FileUtils
require 'socket'
require 'webmock/rspec'

RSpec.configure do |config|
config.order = :random
Kernel.srand config.seed

config.before do
WebMock.disable_net_connect!
config.before :each do
WebMock.disable_net_connect!(allow_localhost: true)

stub_request(:get, 'https://rubygems.org/').to_return(
status: 200,
body: File.read(File.expand_path('support/fixtures/rubygems.org.html', __dir__)),
headers: {}
)
end
end

config.before :suite do
FileUtils.rm_r cache_dir, force: true, secure: true
end

config.after :suite do
FileUtils.rm_r cache_dir, force: true, secure: true
end

def cache_dir
File.join __dir__, 'cache'
end

def fixtures_dir
File.join __dir__, 'fixtures'
end

def fixture_file path
File.join fixtures_dir, path
end

def with_local_webserver host = resolve_localhost, port = 9876
base_dir = fixtures_dir
server = TCPServer.new host, port
server_thread = Thread.start do
Thread.current[:requests] = requests = []
while (session = server.accept)
requests << (request = session.gets)
if %r/^GET (\S+) HTTP\/1\.1$/ =~ request.chomp
resource = (resource = $1) == '' ? '.' : resource
else
session.print %(HTTP/1.1 405 Method Not Allowed\r\nContent-Type: text/plain\r\n\r\n)
session.print %(405 - Method not allowed\r\n)
session.close
next
end
resource, _query = resource.split '?', 2 if resource.include? '?'
if File.file? (resource_file = (File.join base_dir, resource))
if (ext = (File.extname resource_file)[1..-1])
mimetype = %(image/#{ext})
else
mimetype = 'text/plain'
end
session.print %(HTTP/1.1 200 OK\r\nContent-Type: #{mimetype}\r\n\r\n)
File.open resource_file, 'rb:utf-8:utf-8' do |fd|
session.write fd.read 256 until fd.eof?
end
else
session.print %(HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\n)
session.print %(404 - Resource not found.\r\n)
end
session.close
end
end
begin
yield %(http://#{host}:#{port}), server_thread
ensure
server_thread.exit
server_thread.value
server.close
end
end

def resolve_localhost
Socket.ip_address_list.find(&:ipv4?).ip_address
end
end