Skip to content

Commit

Permalink
Merge pull request #27 from mamantoha/set-proxy
Browse files Browse the repository at this point in the history
add proxy support
  • Loading branch information
Kanezoh authored Apr 26, 2024
2 parents 48001b0 + 64da39e commit d4f7054
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ license: MIT
dependencies:
lexbor:
github: kostya/lexbor
http_proxy:
github: mamantoha/http_proxy

development_dependencies:
webmock:
Expand Down
17 changes: 17 additions & 0 deletions spec/proxy_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require "./spec_helper"

WebMock.stub(:get, "http://example.com/with_proxy").to_return(body: "success")

describe "Mechanize proxy test" do
it "set proxy" do
with_proxy_server do |host, port, wants_close|
agent = Mechanize.new
agent.set_proxy("127.0.0.1", 8080)
page = agent.get("http://example.com/with_proxy")
page.body.should eq("success")
page.code.should eq(200)
ensure
wants_close.send(nil)
end
end
end
20 changes: 20 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "spec"
require "webmock"
require "http_proxy"
require "../src/mechanize"

WebMock.stub(:get, "example.com")
Expand Down Expand Up @@ -46,3 +47,22 @@ WebMock.stub(:post, "example.com/post_path")
WebMock.stub(:post, "example.com/post_path")
.with(body: "name=foo&email=bar&commit=submit", headers: {"Content-Type" => "application/x-www-form-urlencoded"})
.to_return(body: "success with button")

def with_proxy_server(host = "127.0.0.1", port = 8080, &)
wants_close = Channel(Nil).new
server = HTTP::Proxy::Server.new

spawn do
server.bind_tcp(host, port)
server.listen
end

spawn do
wants_close.receive
server.close
end

Fiber.yield

yield host, port, wants_close
end
6 changes: 6 additions & 0 deletions src/mechanize.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require "log"
require "uri"
require "http/client"
require "lexbor"
require "http_proxy"
require "./mechanize/http/agent"
require "./mechanize/form"
require "./mechanize/node"
Expand Down Expand Up @@ -305,6 +306,11 @@ class Mechanize
@agent.add_auth(uri, user, pass)
end

# Sets the proxy +address+ at +port+ with an optional +user+ and +password+
def set_proxy(address : String, port : Int32, user : String? = nil, password : String? = nil)
@agent.set_proxy(address, port, user, password)
end

# Runs given block, then resets the page history as it was before.
private def transact
# save the previous history status.
Expand Down
7 changes: 7 additions & 0 deletions src/mechanize/http/agent.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Mechanize
getter authenticate_methods : Hash(URI, Hash(String, Array(AuthRealm)))
getter authenticate_parser : WWWAuthenticateParser

@proxy : ::HTTP::Proxy::Client?

def initialize(@context : Mechanize? = nil)
@history = History.new
@request_headers = ::HTTP::Headers.new
Expand Down Expand Up @@ -43,6 +45,7 @@ class Mechanize
uri, params = resolve_parameters(uri, method, params)
client = ::HTTP::Client.new(uri)
request_auth client, uri
client.set_proxy(@proxy) if @proxy

Check failure on line 48 in src/mechanize/http/agent.cr

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, latest)

expected argument #1 to 'HTTP::Client#set_proxy' to be HTTP::Proxy::Client, not (HTTP::Proxy::Client | Nil)

Check failure on line 48 in src/mechanize/http/agent.cr

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, nightly)

expected argument #1 to 'HTTP::Client#set_proxy' to be HTTP::Proxy::Client, not (HTTP::Proxy::Client | Nil)

Check failure on line 48 in src/mechanize/http/agent.cr

View workflow job for this annotation

GitHub Actions / test (macos-latest)

expected argument #1 to 'HTTP::Client#set_proxy' to be HTTP::Proxy::Client, not (HTTP::Proxy::Client | Nil)
response = http_request(client, uri, method, params, body)
body = response.not_nil!.body
page = response_parse(response, body, uri)
Expand Down Expand Up @@ -170,6 +173,10 @@ class Mechanize
@auth_store.add_auth(uri, user, pass)
end

def set_proxy(address : String, port : Int32, user : String? = nil, password : String? = nil)
@proxy = ::HTTP::Proxy::Client.new(address, port, username: user, password: password)
end

private def set_request_headers(uri, headers)
reset_request_header_cookies
headers.each do |k, v|
Expand Down

0 comments on commit d4f7054

Please sign in to comment.