11require 'test_helper'
2+ require 'webrick'
23
34class HttpClientTest < Test ::Unit ::TestCase
45
56 def test_pluggable_http_client
67 oai_response = <<-eos
78 <Identify>
89 <repositoryName>Mock OAI Provider</repositoryName>
9- <baseURL>http://nowhere.example.com</baseURL>
10+ <baseURL>http://nowhere.example.com</baseURL>
1011 </Identify>
1112eos
1213
@@ -20,7 +21,94 @@ def test_pluggable_http_client
2021
2122 assert_kind_of OAI ::IdentifyResponse , response
2223 assert_equal 'Mock OAI Provider [http://nowhere.example.com]' , response . to_s
23-
24+
25+ end
26+
27+ def test_http_client_handles_trailing_slash_redirects
28+ # First, test that this works when mocking out Faraday client
29+ oai_response = <<-eos
30+ <Identify>
31+ <repositoryName>Mock OAI Provider</repositoryName>
32+ <baseURL>http://nowhere.example.com</baseURL>
33+ </Identify>
34+ eos
35+
36+ stubs = TrailingSlashAwareStubs . new do |stub |
37+ stub . get ( '/oai/?verb=Identify' ) { [ 200 , { } , oai_response ] }
38+ stub . get ( '/oai?verb=Identify' ) {
39+ [ 301 , {
40+ 'Location' => 'http://localhost:3334/oai/?verb=Identify'
41+ } , '' ]
42+ }
43+ end
44+
45+ faraday_stub = Faraday . new do |builder |
46+ require 'faraday_middleware'
47+ builder . use FaradayMiddleware ::FollowRedirects
48+ builder . adapter :test , stubs
49+ end
50+
51+ client = OAI ::Client . new 'http://localhost:3334/oai' , :http => faraday_stub
52+ response = client . identify
53+
54+ assert_kind_of OAI ::IdentifyResponse , response
55+ assert_equal 'Mock OAI Provider [http://nowhere.example.com]' , response . to_s
56+ assert_equal 2 , stubs . consumed [ :get ] . length
57+ assert_equal stubs . consumed [ :get ] . first . path , '/oai'
58+ assert_equal stubs . consumed [ :get ] . last . path , '/oai/'
59+
60+ # Now try it with a real server and default Faraday client
61+ TrailingSlashProviderServer . wrap ( 3334 ) do |server |
62+ client = OAI ::Client . new "http://localhost:#{ server . port } /oai"
63+ response = client . identify
64+
65+ assert_kind_of OAI ::IdentifyResponse , response
66+ assert_equal 'Complex Provider [http://localhost]' , response . to_s
67+ assert_equal 2 , server . consumed . length
68+ assert_equal server . consumed . first . path , '/oai'
69+ assert_equal server . consumed . last . path , '/oai/'
70+ end
71+ end
72+
73+ private
74+
75+ class TrailingSlashProviderServer < ProviderServer
76+ def server_proc
77+ Proc . new do |req , res |
78+ @consumed << req
79+ case req . path
80+ when "/oai/"
81+ begin
82+ res . body = @provider . process_request ( req . query )
83+ res . status = 200
84+ res [ 'Content-Type' ] = 'text/xml'
85+ rescue => err
86+ puts err
87+ puts err . backtrace . join ( "\n " )
88+ res . body = err . backtrace . join ( "\n " )
89+ res . status = 500
90+ end
91+ else
92+ res . body = ''
93+ res . status = 301
94+ res [ 'Location' ] = "http://localhost:#{ port } /oai/?#{ req . query_string } "
95+ end
96+ res
97+ end
98+ end
99+ end
100+
101+ class TrailingSlashAwareStubs < Faraday ::Adapter ::Test ::Stubs
102+ attr_reader :consumed
103+
104+ # ensure leading, but not trailing slash
105+ def normalize_path ( path )
106+ path = '/' + path if path . index ( '/' ) != 0
107+ #path = path.sub('?', '/?')
108+ #path = path + '/' unless $&
109+ path . gsub ( '//' , '/' )
110+ end
111+
24112 end
25113end
26114
0 commit comments