Skip to content
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

Rl feature/support ignore request #131

Merged
merged 19 commits into from
Oct 12, 2021

Conversation

braindeaf
Copy link
Contributor

@braindeaf braindeaf commented Oct 11, 2021

Came across a requirement today to ignore_request? based on the actual request. Sometimes path alone is not enough. This allows testing of the Rack::Request.

# config/initializers/grover.rb
Grover.configure do |config|
  # assigning a Proc
  config.ignore_request = ->(req) do
    req.host == 'www.example.com'
  end
  # matches `www.example.com/foo/bar/123.png`

  config.ignore_request = ->(req) do
    req.has_header?('X-BLOCK')
  end
  # matches `HTTP Header X-BLOCK`
end

Copy link
Contributor

@abrom abrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change looks good @braindeaf 👍

A few minor issues with the specs though. Let me know if you need any more info

response = get 'http://www.example.org/foobarbar', {}, { 'X-BLOCK' => '1' }
expect(response.content_type).to eq 'text/html'

response = get 'http://www.therealexample.org/foobarbar.pdf'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally this should match the request on L890 (aside from the "block" header) so as to minimise the things changing between success and failure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I think I got you.

  1. blocking with host (html request) returns html
    2.blocking with host (pdf request) returns html
  2. not blocking with host (html request) returns html
  3. not blocking with host (pdf request) returns pdf

response = get 'http://www.example.org/foobazbar.pdf', {}, { 'X-BLOCK' => '1' }
expect(response.content_type).to eq 'text/html'

response = get 'http://www.example.org/foobarbar', {}, { 'X-BLOCK' => '1' }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what this is adding to the tests? Ideally this and the next test would both NOT have the extra header. And I would suggest that the URI requested be the same for all (except for the .pdf extension where appropriate!)

Copy link
Contributor Author

@braindeaf braindeaf Oct 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I think I got you.

  1. blocking with header (html request) returns html
  2. blocking with header (pdf request) returns html
  3. not blocking with header (html request) returns html
  4. not blocking with header (pdf request) returns pdf

response = get 'http://www.example.org/foobarbar'
expect(response.content_type).to eq 'text/html'

response = get 'http://www.therealexample.org/foobarbar.pdf'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same applies to these tests. The first two are www.example.org, the second two should be something different (not just this one). And I would also suggest that the path (except the extension where appropriate) should be the same. ie foobazbar[.pdf]

Copy link
Contributor Author

@braindeaf braindeaf Oct 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I think I got you.

  1. blocking with host (html request) returns html
  2. blocking with host (pdf request) returns html
  3. not blocking with host (html request) returns html
  4. not blocking with host (pdf request) returns pdf

README.md Outdated
@@ -270,8 +270,7 @@ N.B. by default PNG and JPEG are not modified in the middleware to prevent break
To enable them, there are configuration options for each image type as well as an option to disable the PDF middleware
(on by default).

If either of the image handling middleware options are enabled, the [ignore_path](#ignore_path) should also
be configured, otherwise assets are likely to be handled which would likely result in 404 responses.
If either of the image handling middleware options are enabled, the [ignore_path](#ignore_path) and/or [ignore_request](#ignore_request) should also be configured, otherwise assets are likely to be handled which would likely result in 404 responses.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please split these over multiple lines. Although it doesn't make a difference for the rendered markdown, it's easier to read in most editors if the line length is kept under 120.

Suggested change
If either of the image handling middleware options are enabled, the [ignore_path](#ignore_path) and/or [ignore_request](#ignore_request) should also be configured, otherwise assets are likely to be handled which would likely result in 404 responses.
If either of the image handling middleware options are enabled, the [ignore_path](#ignore_path) and/or
[ignore_request](#ignore_request) should also be configured, otherwise assets are likely to be handled
which would likely result in 404 responses.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@braindeaf braindeaf requested a review from abrom October 12, 2021 08:53
Copy link
Contributor

@abrom abrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like your specs were still changing multiple things. You should always try to just change one thing and see that the change has the desired outcome

spec/grover/middleware_spec.rb Outdated Show resolved Hide resolved
spec/grover/middleware_spec.rb Outdated Show resolved Hide resolved
Copy link
Contributor

@abrom abrom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @braindeaf 👍

@abrom
Copy link
Contributor

abrom commented Oct 12, 2021

FYI it looks like the recent Let's Encrypt certificate expiry is affecting the build system. I've got another PR in the works that should fix that (#132)

@braindeaf
Copy link
Contributor Author

Excellent, thank you :)

@abrom abrom merged commit 1293a9c into Studiosity:main Oct 12, 2021
@abrom
Copy link
Contributor

abrom commented Oct 12, 2021

Released in v1.0.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants