File: test_server.rb

package info (click to toggle)
ruby-adsf 1.4.6%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 308 kB
  • sloc: ruby: 804; makefile: 12
file content (165 lines) | stat: -rw-r--r-- 4,747 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# frozen_string_literal: true

require 'helper'

class Adsf::Test::Server < MiniTest::Test
  include Rack::Test::Methods
  include Adsf::Test::Helpers

  def run_server(opts = {})
    opts = { root: 'output', port: 50_386 }.merge(opts)
    server = Adsf::Server.new(**opts)
    thread = Thread.new do
      Thread.current.abort_on_exception = true
      server.run
    end

    # Wait for server to start up
    20.times do |i|
      begin
        Net::HTTP.get('127.0.0.1', '/', 50_386)
      rescue Errno::ECONNREFUSED, Errno::ECONNRESET
        sleep(0.1 * 1.2**i)
        retry
      end
      break
    end

    yield
  ensure
    server.stop
    thread.join
  end

  def setup
    super
    FileUtils.mkdir_p('output')
  end

  def test_default_config__serve_index_html
    File.write('output/index.html', 'Hello there! Nanoc loves you! <3')
    run_server do
      assert_equal 'Hello there! Nanoc loves you! <3', Net::HTTP.get('127.0.0.1', '/', 50_386)
    end
  end

  def test_default_config__serve_index_html_in_subdir
    FileUtils.mkdir_p('output/foo')
    File.write('output/foo/index.html', 'Hello there! Nanoc loves you! <3')
    run_server do
      assert_equal 'Hello there! Nanoc loves you! <3', Net::HTTP.get('127.0.0.1', '/foo/', 50_386)
    end
  end

  def test_default_config__serve_index_html_in_subdir_missing_slash
    FileUtils.mkdir_p('output/foo')
    File.write('output/foo/index.html', 'Hello there! Nanoc loves you! <3')
    run_server do
      response = Net::HTTP.get_response('127.0.0.1', '/foo', 50_386)
      assert_equal '302', response.code
      assert_equal 'http://127.0.0.1:50386/foo/', response['Location']
    end
  end

  def test_explicit_handler__serve_index_html
    File.write('output/index.html', 'Hello there! Nanoc loves you! <3')
    run_server(handler: :webrick) do
      assert_equal 'Hello there! Nanoc loves you! <3', Net::HTTP.get('127.0.0.1', '/', 50_386)
    end
  end

  def test_default_config__no_serve_index_xhtml
    File.write('output/index.xhtml', 'Hello there! Nanoc loves you! <3')
    run_server do
      assert_equal "File not found: /\n", Net::HTTP.get('127.0.0.1', '/', 50_386)
    end
  end

  def test_default_config__no_serve_wrong_index
    File.write('output/index666.html', 'Hello there! Nanoc loves you! <3')
    run_server do
      assert_equal "File not found: /\n", Net::HTTP.get('127.0.0.1', '/', 50_386)
    end
  end

  def test_index_xhtml_in_index_filenames__serve_index_xhtml
    File.write('output/index.xhtml', 'Hello there! Nanoc loves you! <3')
    run_server(index_filenames: ['index.xhtml']) do
      assert_equal 'Hello there! Nanoc loves you! <3', Net::HTTP.get('127.0.0.1', '/', 50_386)
    end
  end

  def test_access_caching_headers
    run_server do
      response = Net::HTTP.get_response('127.0.0.1', '/', 50_386)
      assert_equal 'max-age=0, stale-if-error=0', response['Cache-Control']
    end
  end

  def test_access_control_allow_origin
    run_server do
      response = Net::HTTP.get_response('127.0.0.1', '/', 50_386)
      assert_equal '*', response['Access-Control-Allow-Origin']
    end
  end

  def test_access_control_allow_headers
    run_server do
      response = Net::HTTP.get_response('127.0.0.1', '/', 50_386)
      assert_equal 'Origin, X-Requested-With, Content-Type, Accept, Range', response['Access-Control-Allow-Headers']
    end
  end

  def test_non_local_interfaces
    ENV.delete('http_proxy')
    addresses = Socket.getifaddrs.map(&:addr).select(&:ipv4?).map(&:ip_address)
    non_local_addresses = addresses - ['127.0.0.1']

    if non_local_addresses.empty?
      skip 'Need non-local network interfaces for this spec'
    end

    run_server do
      assert_raises(Errno::ECONNREFUSED) do
        Net::HTTP.get(non_local_addresses[0], '/', 50_386)
      end
    end
  end

  def run_live_server
    run_server(live: true) { yield }
  end

  def _test_receives_update
    run_live_server do
      ws = Faye::WebSocket::Client.new('ws://127.0.0.1:35729/')

      sleep 0.2
      FileUtils.mkdir_p('output')
      File.write('output/index.html', 'hello thear')

      queue = SizedQueue.new(2)
      ws.on :open do |_event|
      end
      ws.on :message do |event|
        queue << event.data
      end
      ws.on :close do |_event|
      end

      messages = []
      2.times { messages << queue.pop }

      expected_hello_data = {
        'command' => 'hello',
        'protocols' => ['http://livereload.com/protocols/official-7'],
        'serverName' => 'nanoc-view',
      }

      assert_equal expected_hello_data, JSON.parse(messages[0])

      assert_equal 'reload', JSON.parse(messages[1])['command']
      assert_match %r{adsf/tmp/index\.html$}, JSON.parse(messages[1])['path']
    end
  end
end