File: base_spec.rb

package info (click to toggle)
yard 0.9.38-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,736 kB
  • sloc: ruby: 31,680; javascript: 7,658; makefile: 21
file content (91 lines) | stat: -rw-r--r-- 2,719 bytes parent folder | download | duplicates (6)
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
# frozen_string_literal: true

class MyProcCommand < Base
  def initialize(&block)
    self.class.send(:undef_method, :run)
    self.class.send(:define_method, :run, &block)
  end
end

class MyCacheCommand < Base
  def run; cache 'foo' end
end

RSpec.describe YARD::Server::Commands::Base do
  describe "#cache" do
    before do
      @command = MyCacheCommand.new(:adapter => mock_adapter, :caching => true)
      @command.request = mock_request(nil)
    end

    it "does not cache if caching == false" do
      expect(File).not_to receive(:open)
      @command.caching = false
      @command.run
    end

    it "requires document root to cache" do
      expect(File).not_to receive(:open)
      @command.adapter.document_root = nil
      @command.run
    end

    it "caches to path/to/file.html and create directories" do
      expect(FileUtils).to receive(:mkdir_p).with('/public/path/to')
      expect(File).to receive(:open).with('/public/path/to/file.html', anything)
      @command.request.path_info = '/path/to/file.html'
      @command.run
    end
  end

  describe "#redirect" do
    it "returns a valid redirection" do
      cmd = MyProcCommand.new { redirect '/foo' }
      expect(cmd.call(mock_request('/foo'))).to eq(
        [302, {"Content-Type" => "text/html", "Location" => "/foo"}, [""]]
      )
    end
  end

  describe "#call" do
    it "handles a NotFoundError and use message as body" do
      cmd = MyProcCommand.new { raise NotFoundError, "hello world" }
      s, _, b = *cmd.call(mock_request('/foo'))
      expect(s).to eq 404
      expect(b).to eq ["hello world"]
    end

    it "does not use message as body if not provided in NotFoundError" do
      cmd = MyProcCommand.new { raise NotFoundError }
      s, _, b = *cmd.call(mock_request('/foo'))
      expect(s).to eq 404
      expect(b).to eq ["Not found: /foo"]
    end

    it "handles 404 status code from #run" do
      cmd = MyProcCommand.new { self.status = 404 }
      s, _, b = *cmd.call(mock_request('/foo'))
      expect(s).to eq 404
      expect(b).to eq ["Not found: /foo"]
    end

    it "does not override body if status is 404 and body is defined" do
      cmd = MyProcCommand.new { self.body = "foo"; self.status = 404 }
      s, _, b = *cmd.call(mock_request('/bar'))
      expect(s).to eq 404
      expect(b).to eq ['foo']
    end

    it "handles body as Array" do
      cmd = MyProcCommand.new { self.body = ['a', 'b', 'c'] }
      _, _, b = *cmd.call(mock_request('/foo'))
      expect(b).to eq %w(a b c)
    end

    it "allows headers to be defined" do
      cmd = MyProcCommand.new { headers['Foo'] = 'BAR' }
      _, h, = *cmd.call(mock_request('/foo'))
      expect(h['Foo']).to eq 'BAR'
    end
  end
end