Commit 465a22a

mokha <mo@mokhan.ca>
2019-01-21 20:23:09
add example of soap header and body
1 parent 5b4d821
Changed files (4)
spec/fixtures/soap.builder
@@ -0,0 +1,27 @@
+xml.instruct!
+
+xml.Envelope xmlns: "http://schemas.xmlsoap.org/soap/envelope/" do
+  xml.Header do
+    xml.Security mustUnderstand: '1' do
+      xml.EncryptedKey xmlns: 'http://www.w3.org/2001/04/xmlenc#', Id: key_id do
+        xml.EncryptionMethod Algorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p' do
+          render header_key_info, xml: xml
+          xml.CipherData do
+            xml.CipherValue ''
+          end
+          xml.ReferenceList do
+            xml.DataReference URI: ''
+          end
+        end
+      end
+      xml.BinarySecurityToken ''
+    end
+  end
+  xml.Body xmlns: 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd', Id: id  do
+    encryption_for xml: xml, key_info: body_key_info do |xml|
+      xml.EncryptMe do
+        xml.Secret "secret"
+      end
+    end
+  end
+end
spec/fixtures/soap_header_key_info.builder
@@ -0,0 +1,10 @@
+xml.KeyInfo xmlns: '' do
+  xml.SecurityTokenReference do
+    xml.x509Data do
+      xml.x509IssuerSerial do
+        xml.x509IssuerName ''
+        xml.x509IssuerNumber ''
+      end
+    end
+  end
+end
spec/support/soap.rb
@@ -0,0 +1,39 @@
+# frozen_string_literal: true
+
+class Soap
+  class HeaderKeyInfo
+    include ::Xml::Kit::Templatable
+    attr_accessor :template_path
+
+    def initialize(uri:)
+      @template_path = File.join(__dir__, '../fixtures/soap_header_key_info.builder')
+    end
+  end
+
+  include ::Xml::Kit::Templatable
+
+  attr_reader :id, :signing_key_pair, :encryption_key_pair
+  attr_accessor :template_path
+
+  def initialize
+    @id = ::Xml::Kit::Id.generate
+    @signing_key_pair = ::Xml::Kit::KeyPair.generate(use: :signing)
+    @embed_signature = false
+    @encrypt = true
+    @encryption_key_pair = ::Xml::Kit::KeyPair.generate(use: :encryption)
+    @encryption_certificate = @encryption_key_pair.certificate
+    @template_path = File.join(__dir__, '../fixtures/soap.builder')
+  end
+
+  def key_id
+    'EK-E2C32E59F27A1320A215468956686717'
+  end
+
+  def header_key_info
+    HeaderKeyInfo.new(uri: key_id)
+  end
+
+  def body_key_info
+    ::Xml::Kit::ExternalKeyInfo.new(uri: key_id)
+  end
+end
spec/xml/kit/soap_spec.rb
@@ -0,0 +1,10 @@
+RSpec.describe "Soap Example" do
+  describe "#to_xml" do
+    subject { Soap.new }
+    let(:result) { Hash.from_xml(subject.to_xml) }
+
+    specify { expect(result['Envelope']).to be_present }
+    specify { expect(result['Envelope']['Header']).to be_present }
+    specify { expect(result['Envelope']['Body']).to be_present }
+  end
+end