summaryrefslogtreecommitdiff
path: root/spec/unit/mixin/api_version_request_handling_spec.rb
blob: f3b3fa14abb15fabdfc7d60ef91dc1f55fe6ab76 (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
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
#
# Author:: Tyler Cloke (tyler@chef.io)
# Copyright:: Copyright 2015-2016, Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "spec_helper"

describe Chef::Mixin::ApiVersionRequestHandling do
  let(:dummy_class) { Class.new { include Chef::Mixin::ApiVersionRequestHandling } }
  let(:object) { dummy_class.new }

  describe ".server_client_api_version_intersection" do
    let(:default_supported_client_versions) { [0, 1, 2] }

    context "when the response code is not 406" do
      let(:response) { OpenStruct.new(code: "405") }
      let(:exception) { Net::HTTPClientException.new("405 Something Else", response) }

      it "returns nil" do
        expect(object.server_client_api_version_intersection(exception, default_supported_client_versions))
          .to be_nil
      end

    end # when the response code is not 406

    context "when the response code is 406" do
      let(:response) { OpenStruct.new(code: "406") }
      let(:exception) { Net::HTTPClientException.new("406 Not Acceptable", response) }

      context "when x-ops-server-api-version header does not exist" do
        it "returns nil" do
          expect(object.server_client_api_version_intersection(exception, default_supported_client_versions))
            .to be_nil
        end
      end # when x-ops-server-api-version header does not exist

      context "when x-ops-server-api-version header exists" do
        let(:min_server_version) { 2 }
        let(:max_server_version) { 4 }
        let(:return_hash) do
          {
            "min_version" => min_server_version,
            "max_version" => max_server_version,
          }
        end

        before(:each) do
          allow(response).to receive(:[]).with("x-ops-server-api-version").and_return(Chef::JSONCompat.to_json(return_hash))
        end

        context "when there is no intersection between client and server versions" do
          shared_examples_for "no intersection between client and server versions" do
            it "return an array" do
              expect(object.server_client_api_version_intersection(exception, supported_client_versions))
                .to be_a_kind_of(Array)
            end

            it "returns an empty array" do
              expect(object.server_client_api_version_intersection(exception, supported_client_versions).length)
                .to eq(0)
            end

          end

          context "when all the versions are higher than the max" do
            it_should_behave_like "no intersection between client and server versions" do
              let(:supported_client_versions) { [5, 6, 7] }
            end
          end

          context "when all the versions are lower than the min" do
            it_should_behave_like "no intersection between client and server versions" do
              let(:supported_client_versions) { [0, 1] }
            end
          end

        end # when there is no intersection between client and server versions

        context "when there is an intersection between client and server versions" do
          context "when multiple versions intersect" do
            let(:supported_client_versions) { [1, 2, 3, 4, 5] }

            it "includes all of the intersection" do
              expect(object.server_client_api_version_intersection(exception, supported_client_versions))
                .to eq([2, 3, 4])
            end
          end # when multiple versions intersect

          context "when only the min client version intersects" do
            let(:supported_client_versions) { [0, 1, 2] }

            it "includes the intersection" do
              expect(object.server_client_api_version_intersection(exception, supported_client_versions))
                .to eq([2])
            end
          end # when only the min client version intersects

          context "when only the max client version intersects" do
            let(:supported_client_versions) { [4, 5, 6] }

            it "includes the intersection" do
              expect(object.server_client_api_version_intersection(exception, supported_client_versions))
                .to eq([4])
            end
          end # when only the max client version intersects

        end # when there is an intersection between client and server versions

      end # when x-ops-server-api-version header exists
    end # when the response code is 406

  end # .server_client_api_version_intersection
end # Chef::Mixin::ApiVersionRequestHandling