wsdl_validator 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9961bec661ee4694a60380405b01bde5eda2a839
4
- data.tar.gz: b1a9280dc26806fd4c2fd5e313679f7959dcf696
3
+ metadata.gz: 8a26cd53860d077be92fa6ce8805acbfe00a5197
4
+ data.tar.gz: 0be7f70752fd4f9fb777d409891e92025e1fd922
5
5
  SHA512:
6
- metadata.gz: de6d8ae3431d9ef649f547b06d53dcf24afddb76fd19ea343ca62609dec9cecbf20f6fc5b400b41b90b0560ea85c1dbb8abce31cbbf78abf95c67ba61d4a6d1d
7
- data.tar.gz: 4e2aecefd02a9edb07fe135280764a8bba6603450cdc9da5bdce1a694b05013649c5d1a16d6f980f1cc6bb35b779ce9a63409fb0266d31206fec4a6f0c5c026e
6
+ metadata.gz: 424cacf768ad17fefe1e16be4a1e29a774610ba307663ca1962946730fef0fac2dd2f115c7b06cf121a2adf9bf556e9d76031fa29c384a498d210bd43776224c
7
+ data.tar.gz: bfba1e7cffc896465e8051c93a04f507573e4c3327ceb63494aa28232c8eefb555e1ee9d13559a228edf67e7f348cb6d98206e82c912aa2fd0e24b294f676358
data/README.md CHANGED
@@ -4,7 +4,7 @@ A simple gem to aid with validator a SOAP message according to a WSDL.
4
4
 
5
5
  [![Build Status](https://gitlab.com/samuel-garratt/wsdl_validator/badges/master/build.svg)](https://gitlab.com/samuel-garratt/wsdl_validator/pipelines)
6
6
 
7
- This is a work in progress
7
+ This is a work in progress. If your xml is not being validated corrected please raise an issue.
8
8
 
9
9
  ## Installation
10
10
 
@@ -1,3 +1,3 @@
1
1
  class WsdlValidator
2
- VERSION = '0.1.4'.freeze
2
+ VERSION = '0.1.6'.freeze
3
3
  end
@@ -2,13 +2,25 @@ require_relative 'error'
2
2
 
3
3
  # Helps to validate xml against schemas contained in a WSDL
4
4
  class WsdlValidator
5
- # @param [String] wsdl_url URL to where WSDL is stored
5
+
6
+ attr_accessor :schemas
7
+ attr_accessor :basic_auth
8
+ attr_accessor :doc
9
+
10
+ # Parse WSDL storing authentication and all schemas from it
11
+ # @param [String] wsdl_url URL to where WSDL is stored or location to where file is stored
6
12
  def initialize(wsdl_url)
7
- @doc = Wasabi.document wsdl_url
8
- @schemas = @doc.parser.schemas.collect(&:to_s).join
13
+ self.basic_auth = extract_auth(wsdl_url)
14
+ self.doc = Wasabi.document wsdl_url
15
+ self.schemas = Nokogiri::XML::Schema(parse_wsdl_schemas)
16
+ rescue Wasabi::Resolver::HTTPError => e
17
+ raise WsdlValidator::Error, "Unauthorized for basic auth #{basic_auth}" if e.response.code == 401
18
+ raise e
9
19
  end
10
20
 
11
- # This is not the ideal approach. Ideally Nokogiri parser would be able to understand SOAP xsd as well
21
+ # Gets the namespaces from the SOAP Envelope & Body element, adds them to the root element underneath the body
22
+ # and returns that element.
23
+ # @note This is not the ideal approach. Ideally Nokogiri parser would be able to understand SOAP xsd as well
12
24
  # @return [Nokogiri::XML::Document] Retrieve root element from SOAP
13
25
  def extract_root_from_soap(envelope)
14
26
  body = envelope.children.find { |child| child.name == 'Body' }
@@ -25,10 +37,46 @@ class WsdlValidator
25
37
  xml_under_test = Nokogiri::XML(xml.to_s)
26
38
  soap_envelope = xml_under_test.children.find { |e| e.name == 'Envelope' }
27
39
  xml_under_test = extract_root_from_soap(soap_envelope) if soap_envelope
28
- xsd = Nokogiri::XML::Schema(@schemas)
29
- validator = xsd.validate(xml_under_test)
30
- # validator.each { |error| puts error.message }
40
+ validator = schemas.validate(xml_under_test)
31
41
  raise WsdlValidator::Error, validator.join unless validator.empty?
32
42
  true
33
43
  end
44
+
45
+ private
46
+
47
+ # Returns a string used to prefix a URL for basic auth
48
+ # @return [Array] Format is 'user_name:password@' or empty string
49
+ def extract_auth(wsdl_location)
50
+ return [] unless wsdl_location.to_s =~ /^http|socks/
51
+ url = wsdl_location.is_a?(URI) ? wsdl_location : URI(wsdl_location)
52
+ if url.user
53
+ [url.user, url.password]
54
+ else
55
+ []
56
+ end
57
+ end
58
+
59
+ # Join all the schemas within the WSDL, importing schemas if necessary
60
+ # @return [String] Schemas contained within WSDL
61
+ def parse_wsdl_schemas
62
+ doc.parser.schemas.collect do |schema|
63
+ imports = schema.children.select { |child| child.name == 'import' }
64
+ imported_xsds = imports.collect do |import|
65
+ extract_schema_for(import)
66
+ end.join
67
+ schema = Nokogiri::XML(imported_xsds).children unless imported_xsds.empty?
68
+ schema.to_s
69
+ end.join
70
+ end
71
+
72
+ # Makes a GET request returning the xsd from the defined location
73
+ # @return [String] Schema from location defined in 'schemaLocation' attribute
74
+ def extract_schema_for(import)
75
+ raise WsdlValidator::Error, "Could not find import location from #{import}" unless import['schemaLocation']
76
+ xsd_url = URI(import['schemaLocation'])
77
+ xsd_url.user, xsd_url.password = basic_auth unless basic_auth.empty?
78
+ request = HTTPI::Request.new(xsd_url.to_s)
79
+ HTTPI.get(request).body
80
+ end
81
+
34
82
  end
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.bindir = 'exe'
23
23
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
24
  spec.require_paths = ['lib']
25
+ spec.add_dependency 'httpi'
25
26
  spec.add_dependency 'nokogiri'
26
27
  spec.add_dependency 'wasabi'
27
28
 
@@ -30,4 +31,5 @@ Gem::Specification.new do |spec|
30
31
  spec.add_development_dependency 'rspec', '~> 3.0'
31
32
  spec.add_development_dependency 'simplecov'
32
33
  spec.add_development_dependency 'sinatra'
34
+ spec.add_development_dependency 'sinatra-basic-auth'
33
35
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wsdl_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Garratt
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-26 00:00:00.000000000 Z
11
+ date: 2018-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httpi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: nokogiri
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -108,6 +122,20 @@ dependencies:
108
122
  - - ">="
109
123
  - !ruby/object:Gem::Version
110
124
  version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: sinatra-basic-auth
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
111
139
  description: Makes verifying a SOAP message against a WSDL easy.
112
140
  email:
113
141
  - Samuel.Garratt@integrationqa.co.nz