@@ -126,7 +126,28 @@ def _utf8_page
126126 }
127127 end
128128
129+ def _unencoded_page
130+ lambda { |req , resp |
131+ resp [ 'Content-Type' ] = "text/html"
132+ body = "unencoded-body"
133+ body . force_encoding ( "ASCII-8BIT" ) if body . respond_to? ( :force_encoding )
134+ resp . body = body
135+ }
136+ end
137+
138+ def _badly_encoded_page
139+ lambda { |req , resp |
140+ resp [ 'Content-Type' ] = "text/html; charset=wtf"
141+ body = "badly-encoded-body"
142+ body . force_encoding ( "ASCII-8BIT" ) if body . respond_to? ( :force_encoding )
143+ resp . body = body
144+ }
145+ end
146+
129147 def setup
148+ if defined? ( Encoding . default_external )
149+ @encoding_was = Encoding . default_external
150+ end
130151 @fetcher = OpenID ::StandardFetcher . new
131152 @logfile = StringIO . new
132153 @weblog = WEBrick ::Log . new ( logfile = @logfile )
@@ -152,6 +173,8 @@ def setup
152173 @server . mount_proc ( '/post' , _require_post )
153174 @server . mount_proc ( '/redirect_loop' , _redirect_loop )
154175 @server . mount_proc ( '/utf8_page' , _utf8_page )
176+ @server . mount_proc ( '/unencoded_page' , _unencoded_page )
177+ @server . mount_proc ( '/badly_encoded_page' , _badly_encoded_page )
155178 @server . start
156179 }
157180 @uri = _uri_build
@@ -168,6 +191,9 @@ def _uri_build(path='/')
168191 end
169192
170193 def teardown
194+ if defined? ( Encoding . default_external )
195+ Encoding . default_external = @encoding_was
196+ end
171197 @server . shutdown
172198 # Sleep a little because sometimes this blocks forever.
173199 @server_thread . join
@@ -234,6 +260,30 @@ def test_utf8_page
234260 end
235261 end
236262
263+ def test_unencoded_page
264+ if defined? ( Encoding . default_external )
265+ Encoding . default_external = Encoding ::SHIFT_JIS
266+ end
267+ uri = _uri_build ( '/unencoded_page' )
268+ response = @fetcher . fetch ( uri )
269+ assert_equal ( "unencoded-body" , response . body )
270+ if defined? ( Encoding . default_external )
271+ assert_equal ( Encoding ::US_ASCII , response . body . encoding )
272+ end
273+ end
274+
275+ def test_badly_encoded_page
276+ if defined? ( Encoding . default_external )
277+ Encoding . default_external = Encoding ::SHIFT_JIS
278+ end
279+ uri = _uri_build ( '/badly_encoded_page' )
280+ response = @fetcher . fetch ( uri )
281+ assert_equal ( "badly-encoded-body" , response . body )
282+ if defined? ( Encoding . default_external )
283+ assert_equal ( Encoding ::SHIFT_JIS , response . body . encoding )
284+ end
285+ end
286+
237287 def test_cases
238288 for path , expected_code , expected_url in @@cases
239289 uri = _uri_build ( path )
0 commit comments