-
Notifications
You must be signed in to change notification settings - Fork 47
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
Adding Pact::Jwt object, to handle json web tokens in the pact definintion #32
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require 'pact/symbolize_keys' | ||
module Pact | ||
|
||
# Specifies that the actual object should be considered a match if | ||
# it includes the same keys, and the values of the keys are of the same class. | ||
|
||
class Jwt | ||
include SymbolizeKeys | ||
|
||
attr_reader :contents, :key, :algo | ||
|
||
def initialize contents, key, algo | ||
@contents = contents | ||
@key = key | ||
@algo = algo | ||
end | ||
|
||
def to_hash | ||
{ | ||
:json_class => self.class.name, | ||
:contents => contents, | ||
:algo => algo, | ||
:key => key | ||
} | ||
end | ||
|
||
def as_json opts = {} | ||
to_hash | ||
end | ||
|
||
def to_json opts = {} | ||
as_json.to_json opts | ||
end | ||
|
||
def self.json_create hash | ||
h = symbolize_keys(hash) | ||
new(h[:contents],h[:key],h[:algo]) | ||
end | ||
|
||
def eq other | ||
self == other | ||
end | ||
|
||
def == other | ||
other.is_a?(Jwt) && other.contents == self.contents | ||
end | ||
|
||
def generate | ||
contents | ||
end | ||
end | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
require 'pact/term' | ||
require 'pact/something_like' | ||
require 'pact/jwt' | ||
require 'pact/shared/null_expectation' | ||
require 'pact/shared/key_not_found' | ||
require 'pact/matchers/unexpected_key' | ||
|
@@ -39,6 +40,7 @@ def calculate_diff expected, actual, opts = {} | |
when Regexp then regexp_diff(expected, actual, options) | ||
when Pact::SomethingLike then calculate_diff(expected.contents, actual, options.merge(:type => true)) | ||
when Pact::ArrayLike then array_like_diff(expected, actual, options) | ||
when Pact::Jwt then jwt_diff(expected,actual,options) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dude, spacing! Gotta respect the formatting contentions of the codebase, otherwise your PRs will never get merged! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I know it's just a conversation stater, but it's nice to make a good first impression ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh oops, completely overlooked that. |
||
else object_diff(expected, actual, options) | ||
end | ||
end | ||
|
@@ -61,6 +63,14 @@ def array_diff expected, actual, options | |
end | ||
end | ||
|
||
def jwt_diff expected, actual, options | ||
if actual.is_a? String | ||
diff(expected.contents, JWT.decode(actual,nil,nil)[0], options) | ||
else | ||
Difference.new expected, actual | ||
end | ||
end | ||
|
||
def actual_array_diff expected, actual, options | ||
difference = [] | ||
diff_found = false | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about calling this JWT? It looks funny to my eye, but I know it's completely subjective.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like Jwt very much, to be honest. I started with JWT, and changed it, to avoid confusion with JWT the gem itself.