require "spec_helper" require "rack" describe SocialAvatarProxy::App do subject do Rack::MockRequest.new(app) end let(:app) { SocialAvatarProxy::App.new(options) } let(:options) { Hash.new } let(:successful_response) do response = Net::HTTPSuccess.new("1.1", 200, "Success") response.stub(:stream_check).and_return(true) response.stub(:read_body).and_return("data") response end let(:not_found_response) do Net::HTTPNotFound.new("1.1", 404, "Not Found") end let(:response) { nil } before(:each) do klass = SocialAvatarProxy::Avatar klass.any_instance.stub(:response).and_return(response) end describe "::call" do let(:response) { successful_response } let(:app) { SocialAvatarProxy::App } it "should be callable from the class" do expect(subject.get("/twitter/dave").status).to eq(200) end end describe "#routes" do subject { app } it "should return a object providing the path helpers" do expect(subject.routes.facebook_avatar_path("ryandtownsned")).to match(/^\/.*$/) expect(subject.routes.twitter_avatar_path(2202971)).to match(/^\/.*$/) end end context "given an invalid path" do it "should return a 404 response" do path = "/unknown" expect(subject.get(path).status).to eq(404) end end context "given a valid path" do context "that finds an existing avatar" do let(:response) { successful_response } let(:path) { "/facebook/61413673" } it "should return a 200 response" do expect(subject.get(path).status).to eq(200) end it "should return a Cache-Control header by default" do expect(subject.get(path).headers["Cache-Control"]).to eq("max-stale=86400; max-age=86400; public") end it "should return a Expires header by default" do expect(subject.get(path).headers["Expires"]).to eq((Time.now + 86400).httpdate) end context "given Cache-Control is turned off" do let(:options) do { cache_control: false } end it "should not return a Cache-Control header" do expect(subject.get(path).headers["Cache-Control"]).to be_nil end end context "given Cache-Control is set to private" do let(:options) do { cache_control: { public: false } } end it "should return a Cache-Control header marked for private use" do expect(subject.get(path).headers["Cache-Control"]).to match(/private$/) end end context "given Expires is turned off" do let(:options) do { expires: false } end it "should not return a Cache-Control header" do expect(subject.get(path).headers["Expires"]).to be_nil end end context "given Expires is set to two days" do let(:options) do { expires: (86400 * 2) } end it "should not return a Cache-Control header" do expect(subject.get(path).headers["Expires"]).to eq((Time.now + (86400 * 2)).httpdate) end end end context "that fails to find an avatar" do let(:response) { not_found_response } it "should return a 404 response" do path = "/facebook/someRandomUserThatDoesntExist" expect(subject.get(path).status).to eq(404) end end end end