alephant-broker 3.10.1 → 3.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/lib/alephant/broker/request/batch.rb +3 -3
 - data/lib/alephant/broker/response/asset.rb +1 -1
 - data/lib/alephant/broker/response/base.rb +31 -10
 - data/lib/alephant/broker/response/batch.rb +5 -3
 - data/lib/alephant/broker/version.rb +1 -1
 - data/spec/integration/rack_spec.rb +9 -7
 - metadata +2 -2
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 223acfc17ab349160c5dbf9597213a35b9cdc33a
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: abb1103b13b95e494096b531ad8c109d2c965bdc
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: ec8f97f5911fef1ca5d4bfee7694fee0c9173d4c2275dec58ae3d2161b0d6b19ccf6faee403d00beaa54c7bcaec8dcbeaa80a584d0f45da1e05e0277a6856d28
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 89841c9e7fa8bcc92e2d20da30834e0b63d80f8cca322a0e65b1ad0455390dba3161a61d5ac03ab7d585968b5b693f34ac1be6d3294cfa135359f968be608867
         
     | 
| 
         @@ -10,9 +10,9 @@ module Alephant 
     | 
|
| 
       10 
10 
     | 
    
         
             
                    attr_reader :batch_id, :components, :load_strategy
         
     | 
| 
       11 
11 
     | 
    
         | 
| 
       12 
12 
     | 
    
         
             
                    def initialize(component_factory, env)
         
     | 
| 
       13 
     | 
    
         
            -
                      logger.info "Request::Batch#initialize: id: #{env.data['batch_id']}"
         
     | 
| 
      
 13 
     | 
    
         
            +
                      logger.info "Request::Batch#initialize: id: #{env.data['batch_id']}" if env.data
         
     | 
| 
       14 
14 
     | 
    
         | 
| 
       15 
     | 
    
         
            -
                      @batch_id          = env.data["batch_id"]
         
     | 
| 
      
 15 
     | 
    
         
            +
                      @batch_id          = env.data["batch_id"] if env.data
         
     | 
| 
       16 
16 
     | 
    
         
             
                      @component_factory = component_factory
         
     | 
| 
       17 
17 
     | 
    
         
             
                      @components        = components_for env
         
     | 
| 
       18 
18 
     | 
    
         
             
                    end
         
     | 
| 
         @@ -20,7 +20,7 @@ module Alephant 
     | 
|
| 
       20 
20 
     | 
    
         
             
                    private
         
     | 
| 
       21 
21 
     | 
    
         | 
| 
       22 
22 
     | 
    
         
             
                    def components_for(env)
         
     | 
| 
       23 
     | 
    
         
            -
                      env.data 
     | 
| 
      
 23 
     | 
    
         
            +
                      ((env.data || {}).fetch("components", []) || []).map do |c|
         
     | 
| 
       24 
24 
     | 
    
         
             
                        @component_factory.create(
         
     | 
| 
       25 
25 
     | 
    
         
             
                          c["component"],
         
     | 
| 
       26 
26 
     | 
    
         
             
                          batch_id,
         
     | 
| 
         @@ -9,7 +9,7 @@ module Alephant 
     | 
|
| 
       9 
9 
     | 
    
         
             
                    def initialize(component, request_env)
         
     | 
| 
       10 
10 
     | 
    
         
             
                      @component = component
         
     | 
| 
       11 
11 
     | 
    
         | 
| 
       12 
     | 
    
         
            -
                      @status = component_not_modified(@component.headers, request_env) ?  
     | 
| 
      
 12 
     | 
    
         
            +
                      @status = self.class.component_not_modified(@component.headers, request_env) ? NOT_MODIFIED_STATUS_CODE : component.status
         
     | 
| 
       13 
13 
     | 
    
         | 
| 
       14 
14 
     | 
    
         
             
                      super @status
         
     | 
| 
       15 
15 
     | 
    
         | 
| 
         @@ -12,20 +12,27 @@ module Alephant 
     | 
|
| 
       12 
12 
     | 
    
         | 
| 
       13 
13 
     | 
    
         
             
                    attr_reader :content, :headers, :status
         
     | 
| 
       14 
14 
     | 
    
         | 
| 
      
 15 
     | 
    
         
            +
                    NOT_MODIFIED_STATUS_CODE = 202
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
       15 
17 
     | 
    
         
             
                    STATUS_CODE_MAPPING = {
         
     | 
| 
       16 
     | 
    
         
            -
                      200 
     | 
| 
       17 
     | 
    
         
            -
                       
     | 
| 
       18 
     | 
    
         
            -
                      404 
     | 
| 
       19 
     | 
    
         
            -
                      500 
     | 
| 
      
 18 
     | 
    
         
            +
                      200                      => "ok",
         
     | 
| 
      
 19 
     | 
    
         
            +
                      NOT_MODIFIED_STATUS_CODE => "",
         
     | 
| 
      
 20 
     | 
    
         
            +
                      404                      => "Not found",
         
     | 
| 
      
 21 
     | 
    
         
            +
                      500                      => "Error retrieving content"
         
     | 
| 
       20 
22 
     | 
    
         
             
                    }
         
     | 
| 
       21 
23 
     | 
    
         | 
| 
       22 
24 
     | 
    
         
             
                    def initialize(status = 200, content_type = "text/html")
         
     | 
| 
       23 
25 
     | 
    
         
             
                      @content = STATUS_CODE_MAPPING[status]
         
     | 
| 
       24 
     | 
    
         
            -
                      @headers = { 
     | 
| 
       25 
     | 
    
         
            -
             
     | 
| 
      
 26 
     | 
    
         
            +
                      @headers = {
         
     | 
| 
      
 27 
     | 
    
         
            +
                        "Content-Type"                  => content_type,
         
     | 
| 
      
 28 
     | 
    
         
            +
                        "Access-Control-Allow-Headers"  => "If-None-Match",
         
     | 
| 
      
 29 
     | 
    
         
            +
                        "Access-Control-Allow-Origin"   => "*"
         
     | 
| 
      
 30 
     | 
    
         
            +
                      }
         
     | 
| 
      
 31 
     | 
    
         
            +
                      headers.merge!(Broker.config[:headers]) if Broker.config.has_key?(:headers)
         
     | 
| 
       26 
32 
     | 
    
         
             
                      @status  = status
         
     | 
| 
       27 
33 
     | 
    
         | 
| 
       28 
34 
     | 
    
         
             
                      add_no_cache_headers if should_add_no_cache_headers?(status)
         
     | 
| 
      
 35 
     | 
    
         
            +
                      add_etag_allow_header if headers.has_key?("ETag")
         
     | 
| 
       29 
36 
     | 
    
         
             
                      setup if status == 200
         
     | 
| 
       30 
37 
     | 
    
         
             
                    end
         
     | 
| 
       31 
38 
     | 
    
         | 
| 
         @@ -36,7 +43,7 @@ module Alephant 
     | 
|
| 
       36 
43 
     | 
    
         
             
                    private
         
     | 
| 
       37 
44 
     | 
    
         | 
| 
       38 
45 
     | 
    
         
             
                    def should_add_no_cache_headers?(status)
         
     | 
| 
       39 
     | 
    
         
            -
                      status != 200 && status !=  
     | 
| 
      
 46 
     | 
    
         
            +
                      status != 200 && status != NOT_MODIFIED_STATUS_CODE
         
     | 
| 
       40 
47 
     | 
    
         
             
                    end
         
     | 
| 
       41 
48 
     | 
    
         | 
| 
       42 
49 
     | 
    
         
             
                    def add_no_cache_headers
         
     | 
| 
         @@ -48,10 +55,24 @@ module Alephant 
     | 
|
| 
       48 
55 
     | 
    
         
             
                      log
         
     | 
| 
       49 
56 
     | 
    
         
             
                    end
         
     | 
| 
       50 
57 
     | 
    
         | 
| 
       51 
     | 
    
         
            -
                    def  
     | 
| 
       52 
     | 
    
         
            -
                       
     | 
| 
      
 58 
     | 
    
         
            +
                    def add_etag_allow_header
         
     | 
| 
      
 59 
     | 
    
         
            +
                      headers.merge!({
         
     | 
| 
      
 60 
     | 
    
         
            +
                        "Access-Control-Expose-Headers" => "ETag"
         
     | 
| 
      
 61 
     | 
    
         
            +
                      })
         
     | 
| 
      
 62 
     | 
    
         
            +
                    end
         
     | 
| 
      
 63 
     | 
    
         
            +
             
     | 
| 
      
 64 
     | 
    
         
            +
                    def self.component_not_modified(headers, request_env)
         
     | 
| 
      
 65 
     | 
    
         
            +
                      return false if request_env.if_modified_since.nil? && request_env.if_none_match.nil?
         
     | 
| 
      
 66 
     | 
    
         
            +
             
     | 
| 
      
 67 
     | 
    
         
            +
                      last_modified_match = !request_env.if_modified_since.nil? && headers["Last-Modified"] == request_env.if_modified_since
         
     | 
| 
      
 68 
     | 
    
         
            +
                      etag_match          = !request_env.if_none_match.nil? &&
         
     | 
| 
      
 69 
     | 
    
         
            +
                        unquote_etag(headers["ETag"]) == unquote_etag(request_env.if_none_match)
         
     | 
| 
      
 70 
     | 
    
         
            +
             
     | 
| 
      
 71 
     | 
    
         
            +
                      last_modified_match || etag_match
         
     | 
| 
      
 72 
     | 
    
         
            +
                    end
         
     | 
| 
       53 
73 
     | 
    
         | 
| 
       54 
     | 
    
         
            -
             
     | 
| 
      
 74 
     | 
    
         
            +
                    def self.unquote_etag(etag)
         
     | 
| 
      
 75 
     | 
    
         
            +
                      etag.to_s.gsub(/\A"|"\Z/, '')
         
     | 
| 
       55 
76 
     | 
    
         
             
                    end
         
     | 
| 
       56 
77 
     | 
    
         | 
| 
       57 
78 
     | 
    
         
             
                    def log
         
     | 
| 
         @@ -12,7 +12,7 @@ module Alephant 
     | 
|
| 
       12 
12 
     | 
    
         
             
                    def initialize(components, batch_id, request_env)
         
     | 
| 
       13 
13 
     | 
    
         
             
                      @components = components
         
     | 
| 
       14 
14 
     | 
    
         
             
                      @batch_id   = batch_id
         
     | 
| 
       15 
     | 
    
         
            -
                      @status     = component_not_modified(batch_response_headers, request_env) ?  
     | 
| 
      
 15 
     | 
    
         
            +
                      @status     = self.class.component_not_modified(batch_response_headers, request_env) ? NOT_MODIFIED_STATUS_CODE : 200
         
     | 
| 
       16 
16 
     | 
    
         | 
| 
       17 
17 
     | 
    
         
             
                      super(@status, "application/json")
         
     | 
| 
       18 
18 
     | 
    
         | 
| 
         @@ -45,6 +45,8 @@ module Alephant 
     | 
|
| 
       45 
45 
     | 
    
         
             
                    end
         
     | 
| 
       46 
46 
     | 
    
         | 
| 
       47 
47 
     | 
    
         
             
                    def batch_response_headers
         
     | 
| 
      
 48 
     | 
    
         
            +
                      return {} unless components.count
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
       48 
50 
     | 
    
         
             
                      {
         
     | 
| 
       49 
51 
     | 
    
         
             
                        "ETag"          => batch_response_etag,
         
     | 
| 
       50 
52 
     | 
    
         
             
                        "Last-Modified" => batch_response_last_modified
         
     | 
| 
         @@ -53,10 +55,10 @@ module Alephant 
     | 
|
| 
       53 
55 
     | 
    
         | 
| 
       54 
56 
     | 
    
         
             
                    def batch_response_etag
         
     | 
| 
       55 
57 
     | 
    
         
             
                      etags = components.map do |component|
         
     | 
| 
       56 
     | 
    
         
            -
                        component.headers["ETag"]
         
     | 
| 
      
 58 
     | 
    
         
            +
                        self.class.unquote_etag(component.headers["ETag"])
         
     | 
| 
       57 
59 
     | 
    
         
             
                      end.compact.sort
         
     | 
| 
       58 
60 
     | 
    
         | 
| 
       59 
     | 
    
         
            -
                      Crimp.signature(etags)
         
     | 
| 
      
 61 
     | 
    
         
            +
                      "\"#{Crimp.signature(etags)}\""
         
     | 
| 
       60 
62 
     | 
    
         
             
                    end
         
     | 
| 
       61 
63 
     | 
    
         | 
| 
       62 
64 
     | 
    
         
             
                    def batch_response_last_modified
         
     | 
| 
         @@ -49,6 +49,8 @@ describe Alephant::Broker::Application do 
     | 
|
| 
       49 
49 
     | 
    
         | 
| 
       50 
50 
     | 
    
         
             
              let(:s3_double) { instance_double("Alephant::Storage", :get => content) }
         
     | 
| 
       51 
51 
     | 
    
         | 
| 
      
 52 
     | 
    
         
            +
              let(:not_modified_status_code) { Alephant::Broker::Response::Base::NOT_MODIFIED_STATUS_CODE }
         
     | 
| 
      
 53 
     | 
    
         
            +
             
     | 
| 
       52 
54 
     | 
    
         
             
              before do
         
     | 
| 
       53 
55 
     | 
    
         
             
                allow_any_instance_of(Logger).to receive(:info)
         
     | 
| 
       54 
56 
     | 
    
         
             
                allow_any_instance_of(Logger).to receive(:debug)
         
     | 
| 
         @@ -106,7 +108,7 @@ describe Alephant::Broker::Application do 
     | 
|
| 
       106 
108 
     | 
    
         
             
                  )
         
     | 
| 
       107 
109 
     | 
    
         
             
                end
         
     | 
| 
       108 
110 
     | 
    
         | 
| 
       109 
     | 
    
         
            -
                specify { expect(last_response.status).to eql  
     | 
| 
      
 111 
     | 
    
         
            +
                specify { expect(last_response.status).to eql not_modified_status_code }
         
     | 
| 
       110 
112 
     | 
    
         
             
                specify { expect(last_response.body).to eql "" }
         
     | 
| 
       111 
113 
     | 
    
         
             
                specify { expect(last_response.headers).to_not include("Cache-Control") }
         
     | 
| 
       112 
114 
     | 
    
         
             
                specify { expect(last_response.headers).to_not include("Pragma") }
         
     | 
| 
         @@ -132,7 +134,7 @@ describe Alephant::Broker::Application do 
     | 
|
| 
       132 
134 
     | 
    
         
             
                      :content_type => "test/content",
         
     | 
| 
       133 
135 
     | 
    
         
             
                      :content      => "Test",
         
     | 
| 
       134 
136 
     | 
    
         
             
                      :meta         => {
         
     | 
| 
       135 
     | 
    
         
            -
                        :head_ETag            => "abc",
         
     | 
| 
      
 137 
     | 
    
         
            +
                        :head_ETag            => "\"abc\"",
         
     | 
| 
       136 
138 
     | 
    
         
             
                        :"head_Last-Modified" => "Mon, 11 Apr 2016 09:39:57 GMT"
         
     | 
| 
       137 
139 
     | 
    
         
             
                      }
         
     | 
| 
       138 
140 
     | 
    
         
             
                    )
         
     | 
| 
         @@ -157,7 +159,7 @@ describe Alephant::Broker::Application do 
     | 
|
| 
       157 
159 
     | 
    
         
             
                    end
         
     | 
| 
       158 
160 
     | 
    
         | 
| 
       159 
161 
     | 
    
         
             
                    it "should have ETag cache header" do
         
     | 
| 
       160 
     | 
    
         
            -
                      expect(last_response.headers["ETag"]).to eq("34774567db979628363e6e865127623f")
         
     | 
| 
      
 162 
     | 
    
         
            +
                      expect(last_response.headers["ETag"]).to eq('"34774567db979628363e6e865127623f"')
         
     | 
| 
       161 
163 
     | 
    
         
             
                    end
         
     | 
| 
       162 
164 
     | 
    
         | 
| 
       163 
165 
     | 
    
         
             
                    it "should have most recent Last-Modified header" do
         
     | 
| 
         @@ -206,7 +208,7 @@ describe Alephant::Broker::Application do 
     | 
|
| 
       206 
208 
     | 
    
         
             
                context "when requesting an unmodified response" do
         
     | 
| 
       207 
209 
     | 
    
         
             
                  let(:path)         { "/components/batch" }
         
     | 
| 
       208 
210 
     | 
    
         
             
                  let(:content_type) { "application/json" }
         
     | 
| 
       209 
     | 
    
         
            -
                  let(:etag)         { "34774567db979628363e6e865127623f" }
         
     | 
| 
      
 211 
     | 
    
         
            +
                  let(:etag)         { '"34774567db979628363e6e865127623f"' }
         
     | 
| 
       210 
212 
     | 
    
         | 
| 
       211 
213 
     | 
    
         
             
                  before do
         
     | 
| 
       212 
214 
     | 
    
         
             
                    post(path, batch_json,
         
     | 
| 
         @@ -214,12 +216,12 @@ describe Alephant::Broker::Application do 
     | 
|
| 
       214 
216 
     | 
    
         
             
                      "HTTP_IF_NONE_MATCH" => etag)
         
     | 
| 
       215 
217 
     | 
    
         
             
                  end
         
     | 
| 
       216 
218 
     | 
    
         | 
| 
       217 
     | 
    
         
            -
                  specify { expect(last_response.status).to eql  
     | 
| 
      
 219 
     | 
    
         
            +
                  specify { expect(last_response.status).to eql not_modified_status_code }
         
     | 
| 
       218 
220 
     | 
    
         
             
                  specify { expect(last_response.body).to eq "" }
         
     | 
| 
       219 
221 
     | 
    
         | 
| 
       220 
222 
     | 
    
         
             
                  describe "response should have headers" do
         
     | 
| 
       221 
     | 
    
         
            -
                    it "should  
     | 
| 
       222 
     | 
    
         
            -
                      expect(last_response.headers). 
     | 
| 
      
 223 
     | 
    
         
            +
                    it "should have a Content-Type header" do
         
     | 
| 
      
 224 
     | 
    
         
            +
                      expect(last_response.headers).to include("Content-Type")
         
     | 
| 
       223 
225 
     | 
    
         
             
                    end
         
     | 
| 
       224 
226 
     | 
    
         | 
| 
       225 
227 
     | 
    
         
             
                    it "should have ETag cache header" do
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: alephant-broker
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 3.10. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 3.10.2
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - BBC News
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire:
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date: 2016-04- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2016-04-22 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              requirement: !ruby/object:Gem::Requirement
         
     |