Skip to content

Commit

Permalink
Merge pull request #199 from interagent/feature/test_method_refactor_…
Browse files Browse the repository at this point in the history
…for_upgrade

Add validate_success_only option for test method.
  • Loading branch information
ota42y authored Jan 9, 2019
2 parents fae37cd + 61675e7 commit 017b21e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 16 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,7 @@ describe Committee::Middleware::Stub do
end

def committee_options
json = JSON.parse(File.read("./my-schema.json"))
json = Committee::Drivers::HyperSchema.new.parse(json)

{schema: schema}
@committee_options ||= { schema: Committee::Drivers::load_from_file('docs/schema.json'), prefix: "/v1", validate_success_only: true }
end

describe "GET /" do
Expand Down Expand Up @@ -289,9 +286,12 @@ In committee 3.0 we'll drop many method in method.rb.
So please overwrite committee_options and return schema data and prefix option.
This method should return same data in ResponseValidation option.

The default assertion option in 2.x is `validate_success_only=true`.But we change `validate_success_only=false` in 3.x.
So if you should set false before upgrade 3.x for harmless upgrade.

```ruby
def committee_options
@committee_options ||= {schema: Committee::Drivers::load_from_file('docs/schema.json'), prefix: "/v1"}
@committee_options ||= { schema: Committee::Drivers::load_from_file('docs/schema.json'), prefix: "/v1", validate_success_only: true }
end
```

Expand Down
13 changes: 10 additions & 3 deletions lib/committee/test/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ def assert_schema_content_type

# we use this method 3.0 or later
def committee_options
Committee.warn_deprecated("Committee: committee 3.0 require overwrite committee options so please use this method.")
unless defined?(@call_committee_options_deprecated)
@call_committee_options_deprecated = true
Committee.warn_deprecated("Committee: committee 3.0 require overwrite committee options so please use this method.")
end

{}
end
Expand Down Expand Up @@ -120,13 +123,17 @@ def warn_string_deprecated
def validate_response?(status)
Committee.warn_deprecated("Committee: w'll remove validate_response? method in committee 3.0")

Committee::ResponseValidator.validate?(status)
Committee::ResponseValidator.validate?(status, validate_success_only: validate_success_only)
end

private

def validate_success_only
committee_options.fetch(:validate_success_only, true)
end

def validate?(status)
Committee::ResponseValidator.validate?(status, validate_success_only: true)
Committee::ResponseValidator.validate?(status, validate_success_only: validate_success_only)
end
end
end
23 changes: 20 additions & 3 deletions test/test/methods_new_version_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,24 @@ def committee_options
end

it "detects an invalid response Content-Type" do
@app = new_rack_app(JSON.generate([ValidApp]), {})
@app = new_rack_app(JSON.generate([ValidApp]), 200, {})
get "/apps"
e = assert_raises(Committee::InvalidResponse) do
assert_schema_conform
end
assert_match(/response header must be set to/i, e.message)
end

it "detects an invalid response Content-Type but ignore because it's not success status code" do
@app = new_rack_app(JSON.generate([ValidApp]), 400, {})
get "/apps"
assert_schema_conform
end

it "detects an invalid response Content-Type and check all status code" do
@committee_options.merge!(validate_success_only: false)

@app = new_rack_app(JSON.generate([ValidApp]), 400, {})
get "/apps"
e = assert_raises(Committee::InvalidResponse) do
assert_schema_conform
Expand All @@ -49,10 +66,10 @@ def committee_options

private

def new_rack_app(response, headers={ "Content-Type" => "application/json" })
def new_rack_app(response, status=200, headers={ "Content-Type" => "application/json" })
Rack::Builder.new {
run lambda { |_|
[200, headers, [response]]
[status, headers, [response]]
}
}
end
Expand Down
10 changes: 5 additions & 5 deletions test/test/methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ def committee_schema

describe "#assert_schema_conform" do
it "passes through a valid response" do
mock(Committee).warn_deprecated.with_any_args.times(3)
mock(Committee).warn_deprecated.with_any_args.times(2)

@app = new_rack_app(JSON.generate([ValidApp]))
get "/apps"
assert_schema_conform
end

it "detects an invalid response Content-Type" do
mock(Committee).warn_deprecated.with_any_args.times(3)
mock(Committee).warn_deprecated.with_any_args.times(2)

@app = new_rack_app(JSON.generate([ValidApp]), {})
get "/apps"
Expand All @@ -48,7 +48,7 @@ def committee_schema
end

it "accepts schema string (legacy behavior)" do
mock(Committee).warn_deprecated.with_any_args.times(4)
mock(Committee).warn_deprecated.with_any_args.times(3)

stub(self).committee_schema { nil }
stub(self).schema_contents { JSON.dump(hyper_schema_data) }
Expand All @@ -59,7 +59,7 @@ def committee_schema
end

it "accepts schema hash (legacy behavior)" do
mock(Committee).warn_deprecated.with_any_args.times(4)
mock(Committee).warn_deprecated.with_any_args.times(3)

stub(self).committee_schema { nil }
stub(self).schema_contents { hyper_schema_data }
Expand All @@ -73,7 +73,7 @@ def committee_schema
# Note we don't warn here because this is a recent deprecation and
# passing a schema object will not be a huge performance hit. We should
# probably start warning on the next version.
mock(Committee).warn_deprecated.with_any_args.times(3)
mock(Committee).warn_deprecated.with_any_args.times(2)

stub(self).committee_schema { nil }
stub(self).schema_contents do
Expand Down

0 comments on commit 017b21e

Please sign in to comment.