diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2014-02-17 18:07:50 -0700 |
|---|---|---|
| committer | Dan Allen <dan.j.allen@gmail.com> | 2014-02-17 18:09:36 -0700 |
| commit | a92e2d31cde2ff8df446f3cb7d80fb1a3ff87a39 (patch) | |
| tree | f9a7f4379dfac7c7761499143cb7d83a6951c0f8 /test | |
| parent | bd73800c541590052cc319cfd5faa22750812ef9 (diff) | |
use in-process web server for URI tests
Diffstat (limited to 'test')
| -rw-r--r-- | test/reader_test.rb | 30 | ||||
| -rw-r--r-- | test/test_helper.rb | 48 |
2 files changed, 65 insertions, 13 deletions
diff --git a/test/reader_test.rb b/test/reader_test.rb index a7aceef6..ffc5cd3c 100644 --- a/test/reader_test.rb +++ b/test/reader_test.rb @@ -618,35 +618,39 @@ include::fixtures/no-such-file.ad[] end test 'include directive can retrieve data from uri' do - # disable use of asciidoctor.org due to bug in Rubinius reading zlib compressed response - #url = 'http://asciidoctor.org/humans.txt' - #match = /Asciidoctor/ - url = 'http://echo.jsontest.com/name/asciidoctor' - expect = /\{"name": "asciidoctor"\}/ - + #url = 'http://echo.jsontest.com/name/asciidoctor' + url = %(http://#{Socket.gethostname}:9876/name/asciidoctor) input = <<-EOS .... include::#{url}[] .... EOS - - output = render_embedded_string input, :safe => :safe, :attributes => {'allow-uri-read' => ''} + expect = /\{"name": "asciidoctor"\}/ + output = using_test_webserver do + render_embedded_string input, :safe => :safe, :attributes => {'allow-uri-read' => ''} + end + + assert_not_nil output assert_match(expect, output) end test 'inaccessible uri referenced by include directive does not crash processor' do + url = %(http://#{Socket.gethostname}:9876/no_such_file) input = <<-EOS .... -include::http://127.0.0.1:0[] +include::#{url}[] .... EOS - - begin - output = render_embedded_string input, :safe => :safe, :attributes => {'allow-uri-read' => ''} - assert_match(/Unresolved directive/, output) + + output = begin + using_test_webserver do + render_embedded_string input, :safe => :safe, :attributes => {'allow-uri-read' => ''} + end rescue flunk 'include directive should not raise exception on inaccessible uri' end + assert_not_nil output + assert_match(/Unresolved directive/, output) end test 'include directive supports line selection' do diff --git a/test/test_helper.rb b/test/test_helper.rb index 0064b2fd..5dad3a03 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -8,6 +8,7 @@ end require File.join(ASCIIDOCTOR_PROJECT_DIR, 'lib', 'asciidoctor') require 'test/unit' +require 'socket' require 'nokogiri' autoload :FileUtils, 'fileutils' @@ -261,6 +262,53 @@ class Test::Unit::TestCase $stderr = old_stderr end end + + def using_test_webserver host = Socket.gethostname, port = 9876 + server = TCPServer.new host, port + base_dir = File.expand_path File.dirname __FILE__ + t = Thread.new do + while (session = server.accept) + request = session.gets + resource = nil + if (m = /GET (\S+) HTTP\/1\.1$/.match(request.chomp)) + resource = (resource = m[1]) == '' ? '.' : resource + else + session.print %(HTTP/1.1 405 Method Not Allowed\r\nContent-Type: text/plain\r\n\r\n) + session.print %(405 - Method not allowed\n) + session.close + break + end + + if resource == '/name/asciidoctor' + session.print %(HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n) + session.print %({"name": "asciidoctor"}\n) + elsif File.file?(resource_file = (File.join base_dir, resource)) + session.print %(HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n) + File.open resource_file, 'rb' do |fd| + until fd.eof? do + buffer = fd.read 256 + session.write buffer + end + end + else + session.print %(HTTP/1.1 404 File Not Found\r\nContent-Type: text/plain\r\n\r\n) + session.print %(404 - Resource not found.\n) + end + session.close + end + end + begin + yield + ensure + begin + server.shutdown + # "Errno::ENOTCONN: Socket is not connected' is reported on some platforms; call #close instead of #shutdown + rescue Errno::ENOTCONN + server.close + end + t.exit + end + end end ### |
