Skip to content

Commit d1ffabd

Browse files
author
Nathan Sutton
committed
Switch to rspec to get CI running
1 parent f0c4683 commit d1ffabd

28 files changed

+730
-744
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ Gemfile.lock
99
.zenflow-log
1010
.ruby-gemset
1111
.ruby-version
12+
/.byebug-history
13+
/.tool-versions

Diff for: .rspec

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--require spec_helper

Diff for: Gemfile

+1-16
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
11
source 'https://rubygems.org'
22

3-
gem "multi_json"
4-
5-
group :test do
6-
gem "shoulda", "2.11.3"
7-
gem "mocha"
8-
gem "webmock", "~>1.6.0"
9-
end
10-
11-
group :development do
12-
gem "jruby-openssl", :platforms => :jruby
13-
gem "typhoeus", :platforms => [:mri_18, :mri_19, :mri_20, :mri_21]
14-
end
15-
16-
group :test, :development do
17-
gem "rake"
18-
end
3+
gemspec

Diff for: Rakefile

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
require 'rubygems'
2-
require 'rake'
3-
require 'rake/testtask'
42

5-
Rake::TestTask.new(:test) do |test|
6-
test.libs << 'lib' << 'test'
7-
test.pattern = 'test/**/*_test.rb'
8-
test.verbose = true
3+
begin
4+
require 'rspec/core/rake_task'
5+
RSpec::Core::RakeTask.new(:spec)
6+
task :default => :spec
7+
rescue LoadError
98
end
10-
11-
task :default => :test

Diff for: spec/spec_helper.rb

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
require "rubygems"
2+
require "bundler"
3+
Bundler.setup
4+
Bundler.require(:default, :test, :development)
5+
require 'test/unit/assertions'
6+
require 'webmock/rspec'
7+
8+
RSpec.configure do |config|
9+
config.include Test::Unit::Assertions
10+
11+
def assert_same_elements(a1, a2, msg = nil)
12+
[:select, :inject, :size].each do |m|
13+
[a1, a2].each {|a| assert_respond_to(a, m, "Are you sure that #{a.inspect} is an array? It doesn't respond to #{m}.") }
14+
end
15+
16+
assert a1h = a1.inject({}) { |h,e| h[e] ||= a1.select { |i| i == e }.size; h }
17+
assert a2h = a2.inject({}) { |h,e| h[e] ||= a2.select { |i| i == e }.size; h }
18+
19+
assert_equal(a1h, a2h, msg)
20+
end
21+
22+
config.after(:example) do
23+
ENV["ZENCODER_API_KEY"] = nil
24+
Zencoder.api_key = nil
25+
end
26+
27+
config.expect_with :rspec do |expectations|
28+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
29+
end
30+
31+
config.mock_with :rspec do |mocks|
32+
mocks.verify_partial_doubles = true
33+
end
34+
35+
config.shared_context_metadata_behavior = :apply_to_host_groups
36+
37+
config.filter_run_when_matching :focus
38+
39+
# config.example_status_persistence_file_path = "spec/examples.txt"
40+
41+
config.disable_monkey_patching!
42+
43+
config.warnings = true
44+
45+
if config.files_to_run.one?
46+
config.default_formatter = "doc"
47+
end
48+
49+
# config.profile_examples = 10
50+
51+
config.order = :random
52+
53+
Kernel.srand config.seed
54+
end

Diff for: spec/zencoder/account_spec.rb

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
require "spec_helper"
2+
3+
RSpec.describe Zencoder::Account do
4+
before do
5+
@api_key = 'abc123'
6+
end
7+
8+
describe ".create" do
9+
before do
10+
@url = "#{Zencoder.base_url}/account"
11+
@params = {:api_key => @api_key}
12+
@params_as_json = Zencoder::Serializer.encode(@params)
13+
end
14+
15+
it "POSTs to the correct url and return a response" do
16+
expect(Zencoder::HTTP).to receive(:post).with(@url, @params_as_json, {}).and_return(Zencoder::Response.new)
17+
assert_equal Zencoder::Response, Zencoder::Account.create(@params).class
18+
end
19+
end
20+
21+
describe ".details" do
22+
before do
23+
@url = "#{Zencoder.base_url}/account"
24+
end
25+
26+
it "GETs the correct url and return a response" do
27+
expect(Zencoder::HTTP).to receive(:get).with(@url, :headers => { "Zencoder-Api-Key" => @api_key }).and_return(Zencoder::Response.new)
28+
assert_equal Zencoder::Response, Zencoder::Account.details(:api_key => @api_key).class
29+
end
30+
end
31+
32+
describe ".integration" do
33+
before do
34+
@url = "#{Zencoder.base_url}/account/integration"
35+
end
36+
37+
it "PUTs the correct url and return a response" do
38+
expect(Zencoder::HTTP).to receive(:put).with(@url, nil, :headers => { "Zencoder-Api-Key" => @api_key }).and_return(Zencoder::Response.new)
39+
assert_equal Zencoder::Response, Zencoder::Account.integration(:api_key => @api_key).class
40+
end
41+
end
42+
43+
describe ".live" do
44+
before do
45+
@url = "#{Zencoder.base_url}/account/live"
46+
end
47+
48+
it "PUTs the correct url and return a response" do
49+
expect(Zencoder::HTTP).to receive(:put).with(@url, nil, :headers => { "Zencoder-Api-Key" => @api_key }).and_return(Zencoder::Response.new)
50+
assert_equal Zencoder::Response, Zencoder::Account.live(:api_key => @api_key).class
51+
end
52+
end
53+
end

Diff for: spec/zencoder/http/net_http_spec.rb

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
require "spec_helper"
2+
3+
RSpec.describe Zencoder::HTTP::NetHTTP do
4+
5+
describe "call options" do
6+
it "requests with timeout" do
7+
stub_request(:post, "https://example.com")
8+
expect(Timeout).to receive(:timeout).with(0.001)
9+
Zencoder::HTTP::NetHTTP.post('https://example.com', :timeout => 1)
10+
end
11+
12+
it "requests without timeout" do
13+
stub_request(:post, "https://example.com")
14+
allow(Timeout).to receive(:timeout).and_raise(Timeout::Error)
15+
assert_nothing_raised do
16+
Zencoder::HTTP::NetHTTP.post('https://example.com', :timeout => nil)
17+
end
18+
end
19+
20+
it "adds params to the query string if passed" do
21+
stub_request(:post, "https://example.com/path?some=param")
22+
Zencoder::HTTP::NetHTTP.post('https://example.com/path', {:params => {:some => 'param'}})
23+
end
24+
25+
it "adds params to the existing query string if passed" do
26+
stub_request(:post,'https://example.com/path?original=param&some=param')
27+
Zencoder::HTTP::NetHTTP.post('https://example.com/path?original=param', {:params => {:some => 'param'}})
28+
end
29+
30+
it "adds headers" do
31+
stub_request(:post,'https://example.com/path').with(:headers => {'some' => 'header'})
32+
Zencoder::HTTP::NetHTTP.post('https://example.com/path', {:headers => {:some => 'header'}})
33+
end
34+
35+
it "adds the body to the request" do
36+
stub_request(:post, 'https://example.com/path').with(:body => '{"some": "body"}')
37+
Zencoder::HTTP::NetHTTP.post('https://example.com/path', {:body => '{"some": "body"}'})
38+
end
39+
end
40+
41+
describe "SSL verification" do
42+
before do
43+
@cert_store = double(:add_file => true, :add_path => true, :flags= => true, :set_default_paths => true).as_null_object
44+
@http_stub = double(:use_ssl= => true, :request => true, :verify_mode= => true, :cert_store= => true, :cert_store => @cert_store).as_null_object
45+
expect(::Net::HTTP).to receive(:new).and_return(@http_stub)
46+
end
47+
48+
describe "when set to skip ssl verification" do
49+
it "nots verify" do
50+
expect(@http_stub).to receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
51+
Zencoder::HTTP::NetHTTP.post('https://example.com/path', :skip_ssl_verify => true)
52+
end
53+
54+
it "nots setup a custom cert store" do
55+
expect(@http_stub).to_not receive(:cert_store=)
56+
Zencoder::HTTP::NetHTTP.post('https://example.com/path', :skip_ssl_verify => true)
57+
end
58+
end
59+
60+
describe "when set to do ssl verification" do
61+
it "setups a custom cert store" do
62+
expect(@http_stub).to receive(:cert_store=)
63+
Zencoder::HTTP::NetHTTP.post('https://example.com/path')
64+
end
65+
66+
it "sets the default paths on the custom cert store" do
67+
expect(@cert_store).to receive(:set_default_paths)
68+
Zencoder::HTTP::NetHTTP.post('https://example.com/path')
69+
end
70+
71+
it "sets the ca_file when it is passed in" do
72+
expect(@cert_store).to receive(:add_file).with("/foo/bar/baz.crt")
73+
Zencoder::HTTP::NetHTTP.post('https://example.com/path', :ca_file => "/foo/bar/baz.crt")
74+
end
75+
76+
it "sets the ca_path when it is passed in" do
77+
expect(@cert_store).to receive(:add_path).with("/foo/bar/")
78+
Zencoder::HTTP::NetHTTP.post('https://example.com/path', :ca_path => "/foo/bar/")
79+
end
80+
end
81+
82+
end
83+
84+
describe ".post" do
85+
it "POSTs to specified body to the specified path" do
86+
stub_request(:post, 'https://example.com').with(:body => '{}')
87+
Zencoder::HTTP::NetHTTP.post('https://example.com', :body => '{}')
88+
end
89+
90+
it "POSTs with an empty body if none is provided" do
91+
stub_request(:post, 'https://example.com').with(:body => '')
92+
Zencoder::HTTP::NetHTTP.post('https://example.com')
93+
end
94+
end
95+
96+
describe ".put" do
97+
it "PUTs to specified body to the specified path" do
98+
stub_request(:put, 'https://example.com').with(:body => '{}')
99+
Zencoder::HTTP::NetHTTP.put('https://example.com', :body => '{}')
100+
end
101+
102+
it "PUTs with an empty body if none is provided" do
103+
stub_request(:put, 'https://example.com').with(:body => '')
104+
Zencoder::HTTP::NetHTTP.put('https://example.com')
105+
end
106+
end
107+
108+
describe ".get" do
109+
it "GETs to specified body to the specified path" do
110+
stub_request(:get, 'https://example.com')
111+
Zencoder::HTTP::NetHTTP.get('https://example.com')
112+
end
113+
end
114+
115+
describe ".delete" do
116+
it "DELETEs to specified body to the specified path" do
117+
stub_request(:delete, 'https://example.com')
118+
Zencoder::HTTP::NetHTTP.delete('https://example.com')
119+
end
120+
end
121+
end

Diff for: spec/zencoder/http/typhoeus_spec.rb

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
require "spec_helper"
2+
3+
# Most useless tests ever, but who knows, right?
4+
5+
if !defined?(Typhoeus)
6+
module ::Typhoeus
7+
module Request
8+
end
9+
end
10+
end
11+
12+
RSpec.describe Zencoder::HTTP::Typhoeus do
13+
describe ".post" do
14+
it "POSTs using Typhoeus" do
15+
expect(Typhoeus::Request).to receive(:post).with('https://example.com', {:some => 'options'})
16+
Zencoder::HTTP::Typhoeus.post('https://example.com', {:some => 'options'})
17+
end
18+
end
19+
20+
describe ".put" do
21+
it "PUTs using Typhoeus" do
22+
expect(Typhoeus::Request).to receive(:put).with('https://example.com', {:some => 'options'})
23+
Zencoder::HTTP::Typhoeus.put('https://example.com', {:some => 'options'})
24+
end
25+
end
26+
27+
describe ".get" do
28+
it "GETs using Typhoeus" do
29+
expect(Typhoeus::Request).to receive(:get).with('https://example.com', {:some => 'options'})
30+
Zencoder::HTTP::Typhoeus.get('https://example.com', {:some => 'options'})
31+
end
32+
end
33+
34+
describe ".delete" do
35+
it "DELETEs using Typhoeus" do
36+
expect(Typhoeus::Request).to receive(:delete).with('https://example.com', {:some => 'options'})
37+
Zencoder::HTTP::Typhoeus.delete('https://example.com', {:some => 'options'})
38+
end
39+
end
40+
41+
it "skips ssl verification" do
42+
expect(Typhoeus::Request).to receive(:get).with('https://example.com', {:disable_ssl_peer_verification => true})
43+
Zencoder::HTTP::Typhoeus.get('https://example.com', {:skip_ssl_verify => true})
44+
end
45+
46+
it "uses the path to the cert file" do
47+
expect(Typhoeus::Request).to receive(:get).with('https://example.com', {:disable_ssl_peer_verification => true, :sslcert => "/foo/bar/baz.crt"})
48+
Zencoder::HTTP::Typhoeus.get('https://example.com', {:skip_ssl_verify => true, :ca_file => "/foo/bar/baz.crt"})
49+
end
50+
51+
it "uses the path to the certs directory" do
52+
expect(Typhoeus::Request).to receive(:get).with('https://example.com', {:disable_ssl_peer_verification => true, :capath => "/foo/bar/baz.crt"})
53+
Zencoder::HTTP::Typhoeus.get('https://example.com', {:skip_ssl_verify => true, :ca_path => "/foo/bar/baz.crt"})
54+
end
55+
end

0 commit comments

Comments
 (0)