diff --git a/lib/rack/passbook_rack.rb b/lib/rack/passbook_rack.rb index 4536571..6295290 100644 --- a/lib/rack/passbook_rack.rb +++ b/lib/rack/passbook_rack.rb @@ -9,6 +9,7 @@ def initialize(app) def call(env) @parameters['authToken'] = env['HTTP_AUTHORIZATION'].gsub(/ApplePass /,'') if env['HTTP_AUTHORIZATION'] @parameters.merge!(Rack::Utils.parse_nested_query(env['QUERY_STRING'])) + @parameters['ifModifiedSince'] = env['HTTP_IF_MODIFIED_SINCE'] if env['HTTP_IF_MODIFIED_SINCE'] method_and_params = find_method env['PATH_INFO'] if method_and_params case method_and_params[:method] @@ -25,12 +26,12 @@ def call(env) [response ? 200 : 204, {}, [response.to_json]] when 'latest_pass' response = Passbook::PassbookNotification.latest_pass(method_and_params[:params]) - if response - [200, {'Content-Type' => 'application/vnd.apple.pkpass', - 'Content-Disposition' => 'attachment', - 'filename' => "#{method_and_params[:params]['serialNumber']}.pkpass"}, [response]] + if response[:status] == 200 + [200, {'Content-Type' => 'application/vnd.apple.pkpass', + 'Content-Disposition' => 'attachment', + 'filename' => "#{method_and_params[:params]['serialNumber']}.pkpass","last-modified" => response[:last_modified]}, [response[:latest_pass]]] else - [204, {}, {}] + [response[:status], {}, {}] end when 'log' Passbook::PassbookNotification.passbook_log JSON.parse(env['rack.input'].read 10000) diff --git a/spec/lib/rack/passbook_rack_spec.rb b/spec/lib/rack/passbook_rack_spec.rb index 4a01fb4..4ba68ab 100644 --- a/spec/lib/rack/passbook_rack_spec.rb +++ b/spec/lib/rack/passbook_rack_spec.rb @@ -143,8 +143,8 @@ end context 'get latest pass' do - context 'valid pass' do - let(:raw_pass) {'some url encoded text'} + context 'valid pass updated after if modified since' do + let(:raw_pass) {{:status => 200, :latest_pass => 'some url encoded text', :last_modified => '13713445412'}} before do Passbook::PassbookNotification.should_receive(:latest_pass).with(latest_pass_params). @@ -154,15 +154,25 @@ subject {last_response} its(:status) {should eq 200} - its(:header) {should eq({'Content-Type' => 'application/vnd.apple.pkpass', - 'Content-Disposition' => 'attachment', 'filename' => '27-1.pkpass', 'Content-Length' => '21'})} - its(:body) {should eq raw_pass} + its(:header) {should eq({'Content-Type' => 'application/vnd.apple.pkpass', + 'Content-Disposition' => 'attachment', 'filename' => '27-1.pkpass', 'last-modified' => '13713445412', 'Content-Length' => '21'})} + its(:body) {should eq raw_pass[:latest_pass]} + end + + context 'pass not updated after if modified since' do + before do + Passbook::PassbookNotification.should_receive(:latest_pass).with(latest_pass_params.merge!('ifModifiedSince' => '1371189712')). + and_return({:status => 304}) + get latest_pass_path, {}, rack_env = {'HTTP_IF_MODIFIED_SINCE' => '1371189712'} + end + subject {last_response} + its(:status) {should eq 304} end context 'no pass' do before do Passbook::PassbookNotification.should_receive(:latest_pass).with(latest_pass_params). - and_return(nil) + and_return({:status => 204, :latest_pass => nil}) get latest_pass_path end