Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions stdlib/net-http/0/net-http.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ module Net
# body = 'Some text'
# http.post(path, body) # Can also have a block.
# http.put(path, body)
# http.query(path, body) # Can also have a block.
# http.delete(path)
# http.options(path)
# http.trace(path)
Expand Down Expand Up @@ -222,6 +223,7 @@ module Net
# * #options: OPTIONS.
# * #trace: TRACE.
# * #patch: PATCH.
# * #query, #request_query: QUERY.
#
# * [WebDAV methods](https://en.wikipedia.org/wiki/WebDAV#Implementation):
#
Expand Down Expand Up @@ -561,6 +563,8 @@ module Net
# returns a response object.
# * [#put](rdoc-ref:Net::HTTP#put): Sends a PUT request and returns a response
# object.
# * [#query](rdoc-ref:Net::HTTP#query): Sends a QUERY request and returns a
# response object.
# * [#request](rdoc-ref:Net::HTTP#request): Sends a request and returns a
# response object.
# * [#request_get](rdoc-ref:Net::HTTP#request_get): Sends a GET request and
Expand Down Expand Up @@ -1689,6 +1693,27 @@ module Net
#
def put: (String path, String data, ?headers initheader) -> Net::HTTPResponse

# <!--
# rdoc-file=lib/net/http.rb
# - query(path, data, initheader = nil, dest = nil) { |body_segment| ... }
# -->
# Sends a QUERY request to the server; returns an instance of a subclass of
# Net::HTTPResponse.
#
# The request is based on the Net::HTTP::Query object created from string
# `path`, string `data`, and initial headers hash `initheader`.
#
# data = '{"userId": 1, "id": 1, "title": "delectus aut autem", "completed": false}'
# http = Net::HTTP.new(hostname)
# http.query('/todos/1', data) # => #<Net::HTTPOK 200 OK readbody=true>
#
# Related:
#
# * Net::HTTP::Query: request class for HTTP method QUERY.
# * Net::HTTP.query: sends QUERY request, returns response body.
#
def query: (String path, String data, ?headers initheader, ?bot dest) ?{ (String body_segment) -> void } -> Net::HTTPResponse

# <!--
# rdoc-file=lib/net/http.rb
# - proppatch(path, body, initheader = nil)
Expand Down Expand Up @@ -1918,6 +1943,35 @@ module Net

def request_put: (String path, String data, ?headers initheader) ?{ (Net::HTTPResponse response) -> void } -> Net::HTTPResponse

# <!--
# rdoc-file=lib/net/http.rb
# - request_query(path, data, initheader = nil) { |response| ... }
# -->
# Sends a QUERY request to the server; forms the response into a
# Net::HTTPResponse object.
#
# The request is based on the Net::HTTP::Query object created from string `path`,
# string `data`, and initial headers hash `initheader`.
#
# With no block given, returns the response object:
#
# http = Net::HTTP.new(hostname)
# http.query('/todos')
# # => #<Net::HTTPOK 200 OK readbody=false>
#
# With a block given, calls the block with the response body and returns the
# response object:
#
# http.request_query('/todos') do |res|
# p res
# end # => #<Net::HTTPOK 200 OK readbody=false>
#
# Output:
#
# #<Net::HTTPOK 200 OK readbody=false>
#
def request_query: (String path, String data, ?headers initheader) ?{ (Net::HTTPResponse response) -> void } -> Net::HTTPResponse

# <!--
# rdoc-file=lib/net/http.rb
# - get2(path, initheader = nil)
Expand Down Expand Up @@ -3233,6 +3287,7 @@ module Net
# * Net::HTTP::Options
# * Net::HTTP::Trace
# * Net::HTTP::Patch
# * Net::HTTP::Query
#
# Subclasses for WebDAV requests:
#
Expand Down Expand Up @@ -3576,6 +3631,43 @@ module Net
RESPONSE_HAS_BODY: bool
end

# <!-- rdoc-file=lib/net/http/requests.rb -->
# Class for representing [HTTP method
# QUERY](https://www.rfc-editor.org/rfc/rfc10008.html):
#
# require 'net/http'
# uri = URI('http://example.com')
# hostname = uri.hostname # => "example.com"
# uri.path = '/posts'
# req = Net::HTTP::Query.new(uri) # => #<Net::HTTP::Query QUERY>
# req.body = '{"title": "foo","body": "bar","userId": 1}'
# req.content_type = 'application/json'
# res = Net::HTTP.start(hostname) do |http|
# http.request(req)
# end
#
# See [Request Headers](rdoc-ref:Net::HTTPRequest@Request+Headers).
#
# Properties:
#
# * Request body: yes.
# * Response body: yes.
# * [Safe](https://en.wikipedia.org/wiki/HTTP#Safe_method): yes.
# * [Idempotent](https://en.wikipedia.org/wiki/HTTP#Idempotent_method): yes.
# * [Cacheable](https://en.wikipedia.org/wiki/HTTP#Cacheable_method): yes.
#
# Related:
#
# * Net::HTTP#query: sends `QUERY` request, returns response object.
#
class HTTP::Query < HTTPRequest
METHOD: String

REQUEST_HAS_BODY: bool

RESPONSE_HAS_BODY: bool
end

# <!-- rdoc-file=lib/net/http/requests.rb -->
# Class for representing [WebDAV method
# PROPFIND](http://www.webdav.org/specs/rfc4918.html#METHOD_PROPFIND):
Expand Down
23 changes: 23 additions & 0 deletions test/stdlib/Net_HTTP_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,29 @@ def test_http_verbs
end
end

def test_query
omit_unless Net::HTTP.method_defined?(:query), "Net::HTTP#query requires net-http 0.10.0+"

with_server("localhost") do |uri|
assert_send_type "(String, String) -> Net::HTTPResponse",
Net::HTTP.start("localhost", uri.port), :query, "/todos", "{\"completed\": false}"
assert_send_type "(String, String, Hash[String, String]) -> Net::HTTPResponse",
Net::HTTP.start("localhost", uri.port), :query, "/todos", "{\"completed\": false}", { "Content-Type" => "application/json" }
assert_send_type "(String, String) { (String) -> untyped } -> Net::HTTPResponse",
Net::HTTP.start("localhost", uri.port), :query, "/todos", "{\"completed\": false}" do |string| string end
assert_send_type "(String, String, Hash[String, String]) { (String) -> untyped } -> Net::HTTPResponse",
Net::HTTP.start("localhost", uri.port), :query, "/todos", "{\"completed\": false}", { "Content-Type" => "application/json" } do |string| string end
assert_send_type "(String, String) -> Net::HTTPResponse",
Net::HTTP.start("localhost", uri.port), :request_query, "/todos", "{\"completed\": false}"
assert_send_type "(String, String, Hash[String, String]) -> Net::HTTPResponse",
Net::HTTP.start("localhost", uri.port), :request_query, "/todos", "{\"completed\": false}", { "Content-Type" => "application/json" }
assert_send_type "(String, String) { (Net::HTTPResponse) -> untyped } -> Net::HTTPResponse",
Net::HTTP.start("localhost", uri.port), :request_query, "/todos", "{\"completed\": false}" do |response| response end
assert_send_type "(String, String, Hash[String, String]) { (Net::HTTPResponse) -> untyped } -> Net::HTTPResponse",
Net::HTTP.start("localhost", uri.port), :request_query, "/todos", "{\"completed\": false}", { "Content-Type" => "application/json" } do |response| response end
end
end

def test_request
with_server("localhost") do |uri|
assert_send_type "(String, String) -> Net::HTTPResponse",
Expand Down
Loading