-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved xml encoding/decoding + APIs into :xml plugin
- Loading branch information
1 parent
d45cae0
commit bf9d847
Showing
16 changed files
with
179 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# frozen_string_literal: true | ||
|
||
module HTTPX | ||
module Plugins | ||
# | ||
# This plugin supports request XML encoding/response decoding using the nokogiri gem. | ||
# | ||
# https://gitlab.com/os85/httpx/wikis/XML | ||
# | ||
module XML | ||
MIME_TYPES = %r{\b(application|text)/(.+\+)?xml\b}.freeze | ||
module Transcoder | ||
module_function | ||
|
||
class Encoder | ||
def initialize(xml) | ||
@raw = xml | ||
end | ||
|
||
def content_type | ||
charset = @raw.respond_to?(:encoding) && @raw.encoding ? @raw.encoding.to_s.downcase : "utf-8" | ||
"application/xml; charset=#{charset}" | ||
end | ||
|
||
def bytesize | ||
@raw.to_s.bytesize | ||
end | ||
|
||
def to_s | ||
@raw.to_s | ||
end | ||
end | ||
|
||
def encode(xml) | ||
Encoder.new(xml) | ||
end | ||
|
||
def decode(response) | ||
content_type = response.content_type.mime_type | ||
|
||
raise HTTPX::Error, "invalid form mime type (#{content_type})" unless MIME_TYPES.match?(content_type) | ||
|
||
Nokogiri::XML.method(:parse) | ||
end | ||
end | ||
|
||
class << self | ||
def load_dependencies(*) | ||
require "nokogiri" | ||
end | ||
end | ||
|
||
module ResponseMethods | ||
# decodes the response payload into a Nokogiri::XML::Node object **if** the payload is valid | ||
# "application/xml" (requires the "nokogiri" gem). | ||
def xml | ||
decode(Transcoder) | ||
end | ||
end | ||
|
||
module RequestBodyClassMethods | ||
# ..., xml: Nokogiri::XML::Node #=> xml encoder | ||
def initialize_body(params) | ||
if (xml = params.delete(:xml)) | ||
# @type var xml: Nokogiri::XML::Node | String | ||
return Transcoder.encode(xml) | ||
end | ||
|
||
super | ||
end | ||
end | ||
end | ||
|
||
register_plugin(:xml, XML) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module HTTPX | ||
module Plugins | ||
module XML | ||
MIME_TYPES: Regexp | ||
|
||
module Transcoder | ||
def self?.encode: (Nokogiri::XML::Node | String xml) -> Encoder | ||
def self?.decode: (HTTPX::Response response) -> HTTPX::Transcoder::_Decoder | ||
|
||
class Encoder | ||
@raw: Nokogiri::XML::Node | String # can be nokogiri object | ||
|
||
def content_type: () -> String | ||
|
||
def bytesize: () -> (Integer | Float) | ||
|
||
def to_s: () -> String | ||
|
||
private | ||
|
||
def initialize: (Nokogiri::XML::Node | String xml) -> void | ||
end | ||
end | ||
|
||
module InstanceMethods | ||
end | ||
|
||
module ResponseMethods | ||
def xml: () -> Nokogiri::XML::Node | ||
end | ||
|
||
module RequestBodyClassMethods | ||
end | ||
end | ||
type sessionXML = Session & XML::InstanceMethods | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
require "nokogiri" | ||
|
||
module Requests | ||
module Plugins | ||
module XML | ||
def test_plugin_xml_request_body_document | ||
uri = build_uri("/post") | ||
response = HTTPX.plugin(:xml).post(uri, xml: Nokogiri::XML("<xml></xml>")) | ||
verify_status(response, 200) | ||
body = json_body(response) | ||
verify_header(body["headers"], "Content-Type", "application/xml; charset=utf-8") | ||
# nokogiri in cruby adds \n trailer, jruby doesn't | ||
assert body["data"].start_with?("<?xml version=\"1.0\"?>\n<xml/>") | ||
end | ||
|
||
def test_plugin_xml_request_body_string | ||
uri = build_uri("/post") | ||
response = HTTPX.plugin(:xml).post(uri, xml: "<xml></xml>") | ||
verify_status(response, 200) | ||
body = json_body(response) | ||
verify_header(body["headers"], "Content-Type", "application/xml; charset=utf-8") | ||
assert body["data"] == "<xml></xml>" | ||
end | ||
|
||
def test_plugin_xml_response | ||
uri = build_uri("/xml") | ||
response = HTTPX.plugin(:xml).get(uri) | ||
verify_status(response, 200) | ||
verify_body_length(response) | ||
xml = response.xml | ||
assert xml.is_a?(Nokogiri::XML::Node) | ||
end | ||
end | ||
end | ||
end |