This repository has been archived by the owner on Dec 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #132 from thoughtworks/stenographer
Stenographer logging and playback
- Loading branch information
Showing
16 changed files
with
163 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module Pacto | ||
module Observers | ||
class Stenographer | ||
def initialize(output) | ||
@output = output | ||
end | ||
|
||
def log_investigation(investigation) | ||
return if @output.nil? | ||
|
||
contract = investigation.contract | ||
request = investigation.request | ||
response = investigation.response | ||
name = name_for(contract, request) | ||
values = values_for(contract, request) | ||
|
||
msg = "request #{name.inspect}, values: #{values.inspect}, response: {status: #{response.status}} # #{number_of_citations(investigation)} contract violations" | ||
@output.puts msg | ||
@output.flush | ||
end | ||
|
||
protected | ||
|
||
def name_for(contract, request) | ||
return "Unknown (#{request.uri})" if contract.nil? | ||
contract.name | ||
end | ||
|
||
def number_of_citations(investigation) | ||
return 0 if investigation.nil? | ||
return 0 if investigation.citations.nil? | ||
investigation.citations.size.to_s | ||
end | ||
|
||
def values_for(_contract, request) | ||
# FIXME: Extract vars w/ URI::Template | ||
request.uri.query_values | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"name": "Echo", | ||
"request": { | ||
"headers": { | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"name": "Ping", | ||
"request": { | ||
"headers": { | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'pacto' | ||
Pacto.configure do |c| | ||
c.contracts_path = 'contracts' | ||
end | ||
contracts = Pacto.load_contracts('contracts', 'http://localhost:5000') | ||
contracts.stub_providers | ||
|
||
Pacto.simulate_consumer do | ||
request 'Echo', values: nil, response: { status: 200 } # 0 contract violations | ||
request 'Ping', values: nil, response: { status: 200 } # 0 contract violations | ||
request 'Unknown (http://localhost:8000/404)', values: nil, response: { status: 500 } # 0 contract violations | ||
end | ||
|
||
Pacto.simulate_consumer :my_consumer do | ||
playback 'pacto_stenographer.log' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"name": "Simple Contract", | ||
"request": { | ||
"http_method": "GET", | ||
"path": "/hello", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require 'spec_helper' | ||
|
||
module Pacto | ||
module Observers | ||
describe Stenographer do | ||
let(:pacto_request) { Fabricate(:pacto_request) } | ||
let(:pacto_response) { Fabricate(:pacto_response) } | ||
let(:contract) { Fabricate(:contract) } | ||
let(:citations) { %w(one two) } | ||
let(:investigation) { Pacto::Investigation.new(pacto_request, pacto_response, contract, citations) } | ||
|
||
subject(:stream) { StringIO.new } | ||
|
||
subject { described_class.new stream } | ||
|
||
it 'writes to the stenographer log stream' do | ||
subject.log_investigation investigation | ||
expected_log_line = "request #{contract.name.inspect}, values: {}, response: {status: #{pacto_response.status}} # #{citations.size} contract violations\n" | ||
expect(stream.string).to eq expected_log_line | ||
end | ||
|
||
context 'when the stenographer log stream is nil' do | ||
let(:stream) { nil } | ||
|
||
it 'does nothing' do | ||
# Would raise an error if it tried to write to stream | ||
subject.log_investigation investigation | ||
end | ||
end | ||
end | ||
end | ||
end |