mechanize.cr/spec/agent_spec.cr

82 lines
2.4 KiB
Crystal
Raw Normal View History

2021-06-12 16:17:45 +02:00
require "./spec_helper"
2021-06-13 00:58:50 +02:00
WebMock.stub(:get, "example.com/cookies1").to_return(headers: {"Set-Cookie" => "id=123"})
WebMock.stub(:get, "example.com/cookies2").to_return(headers: {"Set-Cookie" => "name=kanezoh"})
2021-06-15 00:13:29 +02:00
WebMock.stub(:get, "example.com/meta_cookie").to_return(body:
<<-BODY
<html>
<head>
<title>page_title</title>
<meta http-equiv='Set-Cookie' content='id=123;SameSite=None;Secure'>
</head>
<body>
</body>
</html>
BODY
)
2021-06-12 16:17:45 +02:00
describe "Mechanize Agent test" do
2021-06-16 07:17:11 +02:00
it "can fill and submit form" do
2021-06-14 16:41:36 +02:00
agent = Mechanize.new
2021-06-16 16:20:03 +02:00
page = agent.get("http://example.com/form")
2021-06-14 16:41:36 +02:00
form = page.forms[0]
form.field_with("name").value = "foo"
form.field_with("email").value = "bar"
page = agent.submit(form)
page.not_nil!.code.should eq 200
page.not_nil!.body.should eq "success"
end
2021-06-13 00:58:50 +02:00
2021-06-16 07:17:11 +02:00
it "can receive and send cookie" do
2021-06-13 00:58:50 +02:00
agent = Mechanize.new
# receive cookies
agent.get("http://example.com/cookies1")
# send cookies
agent.get("http://example.com/")
agent.request_headers["Cookie"].should eq "id=123"
end
2021-06-16 07:17:11 +02:00
it "can receive and send multiple cookies" do
2021-06-13 00:58:50 +02:00
agent = Mechanize.new
# receive cookies1
agent.get("http://example.com/cookies1")
# receive cookies2
agent.get("http://example.com/cookies2")
agent.get("http://example.com/")
agent.request_headers["Cookie"].should eq "id=123; name=kanezoh"
end
2021-06-13 03:10:06 +02:00
2021-06-16 07:17:11 +02:00
it "can get cookie from meta head" do
2021-06-15 00:13:29 +02:00
agent = Mechanize.new
agent.get("http://example.com/meta_cookie")
agent.get("http://example.com/")
agent.request_headers["Cookie"].should eq "id=123"
end
2021-06-16 07:17:11 +02:00
it "doesn't send cookies to another domain" do
2021-06-13 03:10:06 +02:00
agent = Mechanize.new
agent.get("http://example.com/cookies1")
agent.get("http://example.com/")
agent.request_headers["Cookie"].should eq "id=123"
agent.get("http://another_domain.com/")
agent.request_headers.has_key?("Cookie").should eq false
end
2021-06-16 07:17:11 +02:00
it "can save history" do
agent = Mechanize.new
agent.get("http://example.com/")
agent.history.size.should eq 1
agent.history.last.title.should eq ""
2021-06-16 16:20:03 +02:00
agent.get("http://example.com/form")
2021-06-16 07:17:11 +02:00
agent.history.size.should eq 2
agent.history.last.title.should eq "page_title"
end
2021-06-16 07:22:29 +02:00
it "can back previous page" do
agent = Mechanize.new
agent.get("http://example.com/")
2021-06-16 16:20:03 +02:00
agent.get("http://example.com/form")
2021-06-16 07:22:29 +02:00
agent.current_page.title.should eq "page_title"
agent.back
agent.current_page.title.should eq ""
end
2021-06-12 16:17:45 +02:00
end