diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-11-08 18:13:35 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-11-08 18:13:35 +0300 |
commit | 05db4ead6d5c73cf62ad95d80ccac415bc3bf3cd (patch) | |
tree | 4db17ac5879083050b2b2dad83a60f63fae8b72b /spec/lib/gitlab/webpack/file_loader_spec.rb | |
parent | 6a380347147d1a55afbc6a1c16e04b567ab90d86 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/gitlab/webpack/file_loader_spec.rb')
-rw-r--r-- | spec/lib/gitlab/webpack/file_loader_spec.rb | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/spec/lib/gitlab/webpack/file_loader_spec.rb b/spec/lib/gitlab/webpack/file_loader_spec.rb new file mode 100644 index 00000000000..34d00b9f106 --- /dev/null +++ b/spec/lib/gitlab/webpack/file_loader_spec.rb @@ -0,0 +1,79 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' +require 'support/helpers/file_read_helpers' +require 'support/webmock' + +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 respondes succesfully" do + expect(Gitlab::Webpack::FileLoader.load(file_path)).to be(file_contents) + end + + it "raises error when 404" do + expect { Gitlab::Webpack::FileLoader.load("not_found") }.to raise_error("HTTP error 404") + end + + it "raises error when errors out" do + expect { Gitlab::Webpack::FileLoader.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 { Gitlab::Webpack::FileLoader.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(Gitlab::Webpack::FileLoader.load(file_path)).to be(file_contents) + end + + it "throws error if file cannot be read" do + expect { Gitlab::Webpack::FileLoader.load(error_file_path) }.to raise_error(Gitlab::Webpack::FileLoader::StaticLoadError) + end + end + end +end |