Skip to content

Commit

Permalink
Merge pull request #17 from ninoseki/improve-specs
Browse files Browse the repository at this point in the history
test: change to follow GitLab's testing best practices
  • Loading branch information
ninoseki committed Sep 30, 2018
2 parents 969e23b + 154331e commit 5fb9bb9
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 59 deletions.
2 changes: 1 addition & 1 deletion spec/plugin/cert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe Ryo::Plugin::Cert, :vcr do
subject { Ryo::Plugin::Cert }
describe "#discover" do
describe ".discover" do
it "should return a Hash" do
results = subject.discover "github.com"
expect(results).to be_an(Hash)
Expand Down
2 changes: 1 addition & 1 deletion spec/plugin/dir_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe Ryo::Plugin::Dir do
include_context "http_server"

describe "#discover" do
describe ".discover" do
subject { Ryo::Plugin::Dir }
it "should return an Array" do
results = subject.discover "http://#{host}:#{port}"
Expand Down
2 changes: 1 addition & 1 deletion spec/plugin/dns_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe Ryo::Plugin::DNS, :vcr do
subject { Ryo::Plugin::DNS }
describe "#discover" do
describe ".discover" do
it "should return a Hash" do
results = subject.discover "https:/www.example.com/"
expect(results).to be_an(Hash)
Expand Down
2 changes: 1 addition & 1 deletion spec/plugin/shodan_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe Ryo::Plugin::Shodan, :vcr do
subject { Ryo::Plugin::Shodan }
describe "#discover" do
describe ".discover" do
context "when given a valid API KEY" do
it "should return a Hash" do
results = subject.discover "1.1.1.1"
Expand Down
2 changes: 1 addition & 1 deletion spec/plugin/subdomain/dnsdumpster_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe Ryo::Plugin::Subdomain::DNSDumpster, :vcr do
subject { Ryo::Plugin::Subdomain::DNSDumpster }
describe "#discover" do
describe ".discover" do
context "when given an existing domain" do
it "should return a list of subdomains of a given domain" do
subdomains = subject.discover("example.com")
Expand Down
2 changes: 1 addition & 1 deletion spec/plugin/subdomain/findsubdomain_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe Ryo::Plugin::Subdomain::FindSubDomains, :vcr do
subject { Ryo::Plugin::Subdomain::FindSubDomains }
describe "#discover" do
describe ".discover" do
context "when given an existing domain" do
it "should return a list of subdomains of a given domain" do
subdomains = subject.discover("test.com")
Expand Down
2 changes: 1 addition & 1 deletion spec/plugin/subdomain/subdomain_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe Ryo::Plugin::Subdomain, :vcr do
subject { Ryo::Plugin::Subdomain }
describe "#discover" do
describe ".discover" do
context "when given an existing domain" do
it "should return a list of subdomains of a given domain" do
subdomains = subject.discover("test.com")
Expand Down
2 changes: 1 addition & 1 deletion spec/plugin/tech_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe Ryo::Plugin::Tech, :vcr do
subject { Ryo::Plugin::Tech }
describe "#discover" do
describe ".discover" do
it "should return a Hash" do
results = subject.discover "https://www.webscantest.com/"
expect(results).to be_an(Hash)
Expand Down
2 changes: 1 addition & 1 deletion spec/plugin/whois_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe Ryo::Plugin::Whois, :vcr do
subject { Ryo::Plugin::Whois }
describe "#discover" do
describe ".discover" do
it "should return a Hash" do
results = subject.discover "example.com"
expect(results).to be_an(Hash)
Expand Down
57 changes: 7 additions & 50 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
# frozen_string_literal: true

require "bundler/setup"

require 'coveralls'
Coveralls.wear!

require "dotenv/load"
require "glint"
require "vcr"
require "webrick"

require "ryo"

require_relative "./support/shared_contexts/http_server_shared_context"
require_relative "./support/helpers/helpers"

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"
Expand All @@ -21,55 +24,9 @@
c.syntax = :expect
end

def capture(stream)
begin
stream = stream.to_s
eval "$#{stream} = StringIO.new"
yield
result = eval("$#{stream}").string
ensure
eval("$#{stream} = #{stream.upcase}")
end
result
end
end

def server
server = Glint::Server.new do |port|
http = WEBrick::HTTPServer.new(
BindAddress: "0.0.0.0",
Port: port,
Logger: WEBrick::Log.new(File.open(File::NULL, "w")),
AccessLog: []
)
http.mount_proc("/admin.asp") do |_, res|
body = "test"

res.status = 200
res.content_length = body.size
res.content_type = 'text/plain'
res.body = body
end
trap(:INT) { http.shutdown }
trap(:TERM) { http.shutdown }
http.start
end
Glint::Server.info[:http_server] = {
host: "0.0.0.0",
port: server.port
}
server
end

RSpec.shared_context "http_server" do
before(:all) {
@server = server
@server.start
}
after(:all) { @server.stop }
config.include_context "http_server"

let(:host) { "0.0.0.0" }
let(:port) { @server.port }
config.include Spec::Support::Helpers
end

VCR.configure do |config|
Expand Down
19 changes: 19 additions & 0 deletions spec/support/helpers/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Spec
module Support
module Helpers
def capture(stream)
begin
stream = stream.to_s
eval "$#{stream} = StringIO.new"
yield
result = eval("$#{stream}").string
ensure
eval("$#{stream} = #{stream.upcase}")
end
result
end
end
end
end
42 changes: 42 additions & 0 deletions spec/support/shared_contexts/http_server_shared_context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require "glint"
require "webrick"

RSpec.shared_context "http_server" do
def server
server = Glint::Server.new do |port|
http = WEBrick::HTTPServer.new(
BindAddress: "0.0.0.0",
Port: port,
Logger: WEBrick::Log.new(File.open(File::NULL, "w")),
AccessLog: []
)
http.mount_proc("/admin.asp") do |_, res|
body = "test"

res.status = 200
res.content_length = body.size
res.content_type = 'text/plain'
res.body = body
end
trap(:INT) { http.shutdown }
trap(:TERM) { http.shutdown }
http.start
end
Glint::Server.info[:http_server] = {
host: "0.0.0.0",
port: server.port
}
server
end

before(:all) {
@server = server
@server.start
}
after(:all) { @server.stop }

let(:host) { "0.0.0.0" }
let(:port) { @server.port }
end

0 comments on commit 5fb9bb9

Please sign in to comment.