If the task is checking HTTP responses and following ordinary links, a full browser is not always needed. Ruby Mechanize exposes the proxy on the agent performing those requests:

agent = Mechanize.new('office-route')
begin
  agent.set_proxy('proxy.example', 8080)
  page = agent.get('https://www.example.com/')
  puts page.code
ensure
  agent.shutdown
end

This assumes the mechanize library is loaded. For a permitted public-page check without proxy authentication, the HTTP proxy list provides candidate host/port values for the example. The proxy method also accepts a username and password as its optional third and fourth arguments when the endpoint requires them.

Reuse the configured agent for requests that belong to the same browsing session. It carries cookies and history as well as network settings. Changing the exit proxy does not inherently create a fresh identity or discard a login cookie.

For several proxy sessions, give each its own agent rather than changing one shared agent while work is in progress. The library also documents unique connection names when segregated SSL connections are required; the constructor name in the example makes that choice explicit.

Grouping related URL checks by proxy can reduce needless reconfiguration. Keep the result associated with the route and URL that produced it, including redirects and error responses.

Mechanize does not reproduce a browser's JavaScript execution. Use an actual browser when the behavior under test depends on scripts, rendering or browser-specific authentication. A successful HTTP fetch through the proxy answers the narrower question that this agent was able to make that request.

Related articles