From 3fa6d2db9f528248d9aaa3b5a9fbf1e0c4778d1c Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Fri, 11 Jun 2021 13:47:36 +0900 Subject: [PATCH] add post test --- spec/http_spec.cr | 11 +++++++++++ src/mechanize/form/field.cr | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/spec/http_spec.cr b/spec/http_spec.cr index 3cdfca3..539694d 100644 --- a/spec/http_spec.cr +++ b/spec/http_spec.cr @@ -1,6 +1,9 @@ require "./spec_helper" WebMock.stub(:get, "example.com") WebMock.stub(:get, "http://example.com/?foo=bar&foo1=bar2") +WebMock.stub(:post, "http://example.com/post"). + with(body: "email=foobar", headers: {"Content-Type" => "application/x-www-form-urlencoded"}). + to_return(body: "success") describe "Mechanize HTTP test" do it "simple GET" do @@ -37,4 +40,12 @@ describe "Mechanize HTTP test" do agent.request_headers.empty?.should eq false agent.request_headers["Foo"].should eq "Bar" end + + it "simple POST" do + agent = Mechanize.new + query = { "email" => "foobar" } + page = agent.post("http://example.com/post", query: query) + page.body.should eq "success" + page.code.should eq 200 + end end diff --git a/src/mechanize/form/field.cr b/src/mechanize/form/field.cr index c6ac667..1e077e6 100644 --- a/src/mechanize/form/field.cr +++ b/src/mechanize/form/field.cr @@ -5,10 +5,10 @@ class MechanizeCr::FormContent::Field getter type : String getter raw_value : String - def initialize(node : Node | Myhtml::Node) + def initialize(node : Node | Myhtml::Node, value = "") @node = node @name = node.fetch("name", "") - @value = node.fetch("value", "") + @value = value || node.fetch("value", "") @type = node.fetch("type", "") @raw_value = value end