From 90730cf121e0fdb5ba2785545e0103751ef2297e Mon Sep 17 00:00:00 2001 From: Selva Date: Sun, 3 Jun 2012 20:50:29 +0530 Subject: [PATCH] Jsonify reponds with http cache header of the actual source uri --- lib/jsonify.rb | 5 +++-- spec/jsonify_spec.rb | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/jsonify.rb b/lib/jsonify.rb index b37e058..135e9df 100644 --- a/lib/jsonify.rb +++ b/lib/jsonify.rb @@ -7,7 +7,8 @@ def initialize(route, model) def call(env) path = env['REQUEST_PATH'] response = path == @route ? all : one(find_id(path)) - [200, {"Content-Type" => "application/json"}, [response]] + header = @model.http_cache_header.merge("Content-Type" => "application/json") + [200, header, [response]] end private @@ -23,4 +24,4 @@ def one(id) def find_id(path) path[path.rindex('/') + 1, path.size] end -end \ No newline at end of file +end diff --git a/spec/jsonify_spec.rb b/spec/jsonify_spec.rb index 5aa7997..709227d 100644 --- a/spec/jsonify_spec.rb +++ b/spec/jsonify_spec.rb @@ -2,6 +2,11 @@ require 'test_models' describe Jsonify do + before do + ::Pizza.stubs(:http_cache_header).returns("cache-control" => "private") + ::Pizza.stubs(:all).returns([]) + end + it "should find all objects and convert to json for index url" do pizzas = [{name: 'cheese'}, {name: 'chicken'}] ::Pizza.expects(:all).returns(pizzas) @@ -25,4 +30,12 @@ header['Content-Type'].should == 'application/json' response.first.should == pizza.to_json end -end \ No newline at end of file + + it "should forward the http cache headers" do + ::Pizza.expects(:http_cache_header).returns("cache-control"=>"private") + + jsonify = Jsonify.new('/pizzas', ::Pizza) + status, header, response = jsonify.call('REQUEST_PATH' => '/pizzas') + header['cache-control'].should == "private" + end +end