0
0

More than 1 year has passed since last update.

rspecでETagのテストを追加した

Posted at

背景

Android端末とサーバーの同期(一方通行)を実装するために、ETagを使いました。サーバーのコードを変更したため、テストを追加した

サーバー側

def sync
    return unless stale?(@program)

    render json: { success: 1, data: @program }
  end

Rspec

context "on a subsequent request" do
      before(:each) do
        # first time request
        get api_sync_path, params: { device_id: device.id }
        assert_response 200, response.body
        @etag = response.headers["ETag"]
      end

      context "check ETag exist" do
        it 'both should exist' do
          expect(@etag).to be_present
        end
      end

      context 'when no update' do
        before(:each) do
          @headers = { "ACCEPT" => "application/json", "HTTP_IF_NONE_MATCH" => @etag }
        end

        it 'should return 304' do
          get api_sync_path, params: { device_id: device.id }, headers: @headers
          assert_response 304, response.body
        end
      end

      context 'when there is update' do
        before(:each) do
          @headers = { "ACCEPT" => "application/json", "HTTP_IF_NONE_MATCH" => @etag }
          # program_slot.touchだと時間が短すぎて、変わらない可能性がある
          program_slot.update(updated_at: 1.hour.from_now)           
        end

        it 'should return 200' do
          get api_sync_path, params: { device_id: device.id }, headers: @headers
          assert_response 200, response.body
        end
      end
    end

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0