-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The request options middleware is now used as a centralized place to add request options.
- Loading branch information
1 parent
a89b54b
commit 91f8b2c
Showing
8 changed files
with
53 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule Crawly.Middlewares.RequestOptions do | ||
@moduledoc """ | ||
Request settings middleware | ||
Allows to specify HTTP request settings like follow_redirect, or request | ||
timeout. | ||
### Example Declaration | ||
``` | ||
middlewares: [ | ||
{Crawly.Middlewares.RequestOptions, [timeout: 30_000, recv_timeout: 15000]} | ||
] | ||
``` | ||
""" | ||
@behaviour Crawly.Pipeline | ||
|
||
def run(request, state, options \\ []) do | ||
{%Crawly.Request{request| options: options}, state} | ||
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule Middlewares.OptionsTest do | ||
use ExUnit.Case, async: false | ||
|
||
test "Options are added to request settings" do | ||
req = Crawly.Request.new("http://example.com") | ||
middlewares = [ | ||
{ | ||
Crawly.Middlewares.RequestOptions, | ||
[timeout: 30_000, recv_timeout: 15000] | ||
} | ||
] | ||
state = %{spider_name: :test_spider} | ||
|
||
{new_request, _state} = Crawly.Utils.pipe(middlewares, req, state) | ||
|
||
assert [timeout: 30000, recv_timeout: 15000] == new_request.options | ||
end | ||
end |