Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Savon version 2 requests #7

Closed
wants to merge 8 commits into from
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@

* Initial version. Adds multipart support (SOAP with Attachments) to Savon v0.9.7.
Please test and provide feedback so we can merge this into Savon proper.

## 2.0.0 (UNRELEASED)
* Rewrite of savon-multipart to work with Savon 2.0+
This includes all of the existing functionality in savon-multipart
for savon1, but in a way that will be easier to maintain.
4 changes: 1 addition & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
source :rubygems
source 'https://rubygems.org'
gemspec

#gem "savon", :path => "../savon"
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@ Savon Multipart [![Build Status](https://secure.travis-ci.org/savonrb/savon-mult
===============

Adds multipart support (SOAP with Attachments) to [Savon](https://github.com/savonrb/savon).
Please test and provide feedback so we can merge this into Savon proper.
Please test and provide feedback so we can support as many multipart-soap messages as possible.


Savon2
------

Recently Savon launched a rewrite that broke compatibility with savon-multipart. If you're using savon
version 1, this should just work, but if you'd like to use Savon2, you will probably want to specify
something like this in your Gemfile


```
gem 'savon-multipart', :git => 'https://github.com/tjarratt/savon-multipart.git', :branch => 'savon2'
```


Installation
Expand Down
8 changes: 3 additions & 5 deletions lib/savon-multipart.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "savon"
require "savon/multipart/version"
require "savon/multipart/soap/request"
require "savon/multipart/soap/response"
require "savon/multipart/soap/xml"
require 'savon'
require 'savon/multipart/version'
require 'savon/multipart/response'
50 changes: 50 additions & 0 deletions lib/savon/multipart/response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'mail'

module Savon
module Multipart
class Response < Savon::Response
attr_reader :parts

def initialize(*args)
@parts = []
super
end

def attachments
if multipart?
parse_body unless @has_parsed_body
@parts.attachments
else
[]
end
end

def to_xml
if multipart?
parse_body unless @has_parsed_body
@parts.first.body.to_s
else
super
end
end

private
def multipart?
http.headers['Content-Type'] =~ /^multipart/
end

def boundary
return unless multipart?
@boundary ||= Mail::Field.new('Content-Type', http.headers['Content-Type']).parameters['boundary']
end

def parse_body
@parts = Mail::Part.new(
:headers => http.headers,
:body => http.body
).body.split!(boundary).parts
@has_parsed_body = true
end
end
end
end
14 changes: 0 additions & 14 deletions lib/savon/multipart/soap/part.rb

This file was deleted.

29 changes: 0 additions & 29 deletions lib/savon/multipart/soap/request.rb

This file was deleted.

68 changes: 0 additions & 68 deletions lib/savon/multipart/soap/response.rb

This file was deleted.

50 changes: 0 additions & 50 deletions lib/savon/multipart/soap/xml.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/savon/multipart/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Savon
module Multipart

VERSION = "1.2.0"
VERSION = "2.0.0"

end
end
2 changes: 1 addition & 1 deletion savon-multipart.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Gem::Specification.new do |s|

s.rubyforge_project = s.name

s.add_dependency "savon", "1.2.0"
s.add_dependency "savon", "~> 2.1.0"
s.add_dependency "mail"

s.add_development_dependency "rake", "~> 0.8.7"
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/response/not_multipart.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:wsdl="http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header><ns1:TransactionID soapenv:actor="" soapenv:mustUnderstand="1" xsi:type="xsd:string" xmlns:ns1="http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2">2011012713535811111111111</ns1:TransactionID></soapenv:Header><soapenv:Body><SubmitReq xmlns="http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2"><MM7Version>5.3.0</MM7Version><SenderIdentification><VASPID>messaging</VASPID><VASID>ADM</VASID><SenderAddress><ShortCode>1111</ShortCode></SenderAddress></SenderIdentification><Recipients><To><Number>11111111111</Number></To></Recipients><ServiceCode>1</ServiceCode><MessageClass>Personal</MessageClass><ExpiryDate>2011-01-28T13:53:58Z</ExpiryDate><DeliveryReport>false</DeliveryReport><ReadReply>false</ReadReply><Priority>Normal</Priority><Subject>Test MMS via Savon</Subject><ChargedParty>Sender</ChargedParty><Content href="cid:attachment_1" allowAdaptations="true"/></SubmitReq></soapenv:Body></soapenv:Envelope>
25 changes: 25 additions & 0 deletions spec/fixtures/response/simple_multipart.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
----==_mimepart_4d416ae62fd32_201a8043814c4724
Date: Thu, 27 Jan 2011 13:53:58 +0100
Message-ID: <[email protected]>
Mime-Version: 1.0
Content-Type: text/xml;
charset=utf-8
Content-Transfer-Encoding: 7bit
content-id: soap_xml_part

<?xml version="1.0" encoding="UTF-8"?><envelope ><header></header><body><SubmitReq>true</SubmitReq></body></envelope>

----==_mimepart_4d416ae628621_201a8043814c46ea
Date: Thu, 27 Jan 2011 13:53:58 +0100
Message-ID: <[email protected]>
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
content-id: text_0.txt

This is a test message from Github

----==_mimepart_4d416ae628621_201a8043814c46ea--

----==_mimepart_4d416ae62fd32_201a8043814c4724--
47 changes: 43 additions & 4 deletions spec/savon/soap/response_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "spec_helper"
require 'savon-multipart'

describe Savon::SOAP::Response do
describe Savon::Multipart::Response do

before do
@header = { "Content-Type" => 'multipart/related; boundary="--==_mimepart_4d416ae62fd32_201a8043814c4724"; charset=UTF-8; type="text/xml"' }
Expand All @@ -17,16 +17,55 @@
response.parts[1].parts[2].body.should == "This is a test message from Github"
end

it "returns a String from the #to_xml method" do
body = File.read(File.expand_path('../../../fixtures/response/simple_multipart.txt', __FILE__))
response = soap_response :headers => @header, :body => body
response.to_xml.class.should == String
end

it "returns a Hash from the #body method" do
body = File.read(File.expand_path('../../../fixtures/response/simple_multipart.txt', __FILE__))
response = soap_response :headers => @header, :body => body
response.body.class.should == Hash
response.body.should == {:submit_req => true}
end

it "returns the attachments" do
response = soap_response :headers => @header, :body => @body
response.attachments.size.should == 1
end

it "parses soap messages without attachments too" do
header = { 'Content-Type' => 'text/html; charset=utf-8'}
body = File.read(File.expand_path('../../../fixtures/response/not_multipart.txt', __FILE__))
response = soap_response :headers => header, :body => body

response.to_xml.chomp.should == '<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:wsdl="http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header><ns1:TransactionID soapenv:actor="" soapenv:mustUnderstand="1" xsi:type="xsd:string" xmlns:ns1="http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2">2011012713535811111111111</ns1:TransactionID></soapenv:Header><soapenv:Body><SubmitReq xmlns="http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2"><MM7Version>5.3.0</MM7Version><SenderIdentification><VASPID>messaging</VASPID><VASID>ADM</VASID><SenderAddress><ShortCode>1111</ShortCode></SenderAddress></SenderIdentification><Recipients><To><Number>11111111111</Number></To></Recipients><ServiceCode>1</ServiceCode><MessageClass>Personal</MessageClass><ExpiryDate>2011-01-28T13:53:58Z</ExpiryDate><DeliveryReport>false</DeliveryReport><ReadReply>false</ReadReply><Priority>Normal</Priority><Subject>Test MMS via Savon</Subject><ChargedParty>Sender</ChargedParty><Content href="cid:attachment_1" allowAdaptations="true"/></SubmitReq></soapenv:Body></soapenv:Envelope>'
response.parts.size.should == 0
response.attachments.size.should == 0
end

it "only parses the SOAP body once" do
response = soap_response :headers => @header, :body => @body
response.to_xml

counter = 0
subbed_parse_body = lambda { counter += 1 }
response.class.send(:define_method, :parse_body, subbed_parse_body)
5.times { response.attachments }

counter.should == 0
end

def soap_response(options = {})
defaults = { :code => 200, :headers => {}, :body => "" }
response = defaults.merge options
globals = {
:multipart => true,
:convert_response_tags_to => lambda { |tag| tag.snakecase.to_sym}
}
http = HTTPI::Response.new(response[:code], response[:headers], response[:body])

Savon::SOAP::Response.new Savon.config, HTTPI::Response.new(response[:code], response[:headers], response[:body])
Savon::Multipart::Response.new(http, globals, {})
end

end
10 changes: 0 additions & 10 deletions spec/spec_helper.rb

This file was deleted.