Welcome to mirror list, hosted at ThFree Co, Russian Federation.

file_loader_spec.rb « webpack « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 16bf380fd13a6bcd742be574826030f24a10e33e (plain)
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Webpack::FileLoader do
  include FileReadHelpers
  include WebMock::API

  let(:error_file_path) { "error.yml" }
  let(:file_path) { "my_test_file.yml" }
  let(:file_contents) do
    <<-EOF
    - hello
    - world
    - test
    EOF
  end

  before do
    allow(Gitlab.config.webpack.dev_server).to receive_messages(host: 'hostname', port: 2000, https: false)
    allow(Gitlab.config.webpack).to receive(:public_path).and_return('public_path')
    allow(Gitlab.config.webpack).to receive(:output_dir).and_return('webpack_output')
  end

  context "with dev server enabled" do
    before do
      allow(Gitlab.config.webpack.dev_server).to receive(:enabled).and_return(true)

      stub_request(:get, "http://hostname:2000/public_path/not_found").to_return(status: 404)
      stub_request(:get, "http://hostname:2000/public_path/#{file_path}").to_return(body: file_contents, status: 200)
      stub_request(:get, "http://hostname:2000/public_path/#{error_file_path}").to_raise(StandardError)
    end

    it "returns content when responds successfully" do
      expect(described_class.load(file_path)).to eq(file_contents)
    end

    it "raises error when 404" do
      expect { described_class.load("not_found") }.to raise_error("HTTP error 404")
    end

    it "raises error when errors out" do
      expect { described_class.load(error_file_path) }.to raise_error(Gitlab::Webpack::FileLoader::DevServerLoadError)
    end
  end

  context "with dev server enabled and https" do
    before do
      allow(Gitlab.config.webpack.dev_server).to receive(:enabled).and_return(true)
      allow(Gitlab.config.webpack.dev_server).to receive(:https).and_return(true)

      stub_request(:get, "https://hostname:2000/public_path/#{error_file_path}").to_raise(EOFError)
    end

    it "raises error if catches SSLError" do
      expect { described_class.load(error_file_path) }.to raise_error(Gitlab::Webpack::FileLoader::DevServerSSLError)
    end
  end

  context "with dev server disabled" do
    before do
      allow(Gitlab.config.webpack.dev_server).to receive(:enabled).and_return(false)
      stub_file_read(::Rails.root.join("webpack_output/#{file_path}"), content: file_contents)
      stub_file_read(::Rails.root.join("webpack_output/#{error_file_path}"), error: Errno::ENOENT)
    end

    describe ".load" do
      it "returns file content from file path" do
        expect(described_class.load(file_path)).to be(file_contents)
      end

      it "throws error if file cannot be read" do
        expect { described_class.load(error_file_path) }.to raise_error(Gitlab::Webpack::FileLoader::StaticLoadError)
      end
    end
  end
end