<commit_before>require 'test_helper'

class Api::V1::VersionsControllerTest < ActionController::TestCase
  def get_show(rubygem, format='json')
    get :show, :id => rubygem.name, :format => format
  end

  def get_latest(rubygem, format='json')
    get :latest, :id => rubygem.name, :format => format
  end

  def get_reverse_dependencies(rubygem, options={ :format => 'json' })
    get :reverse_dependencies, options.merge(:id => rubygem.name)
  end

  def set_cache_header(timestamp)
    ims = ActionDispatch::Http::Cache::Request::HTTP_IF_MODIFIED_SINCE
    request.env[ims] = timestamp.httpdate
  end

  def self.should_respond_to(format)
    context "with #{format.to_s.upcase}" do
      should "have a list of versions for the first gem" do
        get_show(@rubygem, format)
        assert_equal 2, yield(@response.body).size
      end

      should "be ordered by position with prereleases" do
        get_show(@rubygem, format)
        arr = yield(@response.body)
        assert_equal "2.0.0", arr.first["number"]
        assert_equal "1.0.0.pre", arr.second["number"]
      end

      should "be ordered by position" do
        get_show(@rubygem2, format)
        arr = yield(@response.body)
        assert_equal "3.0.0", arr.first["number"]
        assert_equal "2.0.0", arr.second["number"]
        assert_equal "1.0.0", arr.third["number"]
      end

      should "have a list of versions for the second gem" do
        get_show(@rubygem2, format)
        assert_equal 3, yield(@response.body).size
      end
    end
  end

  context "on GET to show" do
    setup do
      @rubygem = create(:rubygem)
      create(:version, :rubygem => @rubygem, :number => '2.0.0')
      create(:version, :rubygem => @rubygem, :number => '1.0.0.pre', :prerelease => true)
      create(:version, :rubygem => @rubygem, :number => '3.0.0', :indexed => false)

      @rubygem2 = create(:rubygem)
      create(:version, :rubygem => @rubygem2, :number => '3.0.0')
      create(:version, :rubygem => @rubygem2, :number => '2.0.0')
      create(:version, :rubygem => @rubygem2, :number => '1.0.0')
    end

    should_respond_to(:json) do |body|
      MultiJson.load(body)
    end

    should_respond_to(:yaml) do |body|
      YAML.load(body)
    end

    should "return Last-Modified header" do
      get_show(@rubygem)
      assert_equal @response.headers['Last-Modified'], @rubygem.updated_at.httpdate
    end

    should "return 304 when If-Modified-Since header is satisfied" do
      set_cache_header(@rubygem.updated_at)
      get_show(@rubygem)
      assert_response :not_modified
    end

    should "return 200 when If-Modified-Since header is not satisfied" do
      set_cache_header(@rubygem.updated_at - 1)
      get_show(@rubygem)
      assert_response :success
    end

  end

  context "on GET to show for an unknown gem" do
    setup do
      get :show, :id => "nonexistent_gem", :format => "json"<commit_msg>Better caching test on versions api<commit_after>require 'test_helper'

class Api::V1::VersionsControllerTest < ActionController::TestCase
  def get_show(rubygem, format='json')
    get :show, id: rubygem.name, format: format
  end

  def get_latest(rubygem, format='json')
    get :latest, :id => rubygem.name, :format => format
  end

  def get_reverse_dependencies(rubygem, options={ :format => 'json' })
    get :reverse_dependencies, options.merge(:id => rubygem.name)
  end

  def set_cache_header
    @request.if_modified_since = @response.headers['Last-Modified']
    @request.if_none_match = @response.etag
  end

  def self.should_respond_to(format)
    context "with #{format.to_s.upcase}" do
      should "have a list of versions for the first gem" do
        get_show(@rubygem, format)
        assert_equal 2, yield(@response.body).size
      end

      should "be ordered by position with prereleases" do
        get_show(@rubygem, format)
        arr = yield(@response.body)
        assert_equal "2.0.0", arr.first["number"]
        assert_equal "1.0.0.pre", arr.second["number"]
      end

      should "be ordered by position" do
        get_show(@rubygem2, format)
        arr = yield(@response.body)
        assert_equal "3.0.0", arr.first["number"]
        assert_equal "2.0.0", arr.second["number"]
        assert_equal "1.0.0", arr.third["number"]
      end

      should "have a list of versions for the second gem" do
        get_show(@rubygem2, format)
        assert_equal 3, yield(@response.body).size
      end
    end
  end

  context "on GET to show" do
    setup do
      @rubygem = create(:rubygem)
      create(:version, :rubygem => @rubygem, :number => '2.0.0')
      create(:version, :rubygem => @rubygem, :number => '1.0.0.pre', :prerelease => true)
      create(:version, :rubygem => @rubygem, :number => '3.0.0', :indexed => false)

      @rubygem2 = create(:rubygem)
      create(:version, :rubygem => @rubygem2, :number => '3.0.0')
      create(:version, :rubygem => @rubygem2, :number => '2.0.0')
      create(:version, :rubygem => @rubygem2, :number => '1.0.0')
    end

    should_respond_to(:json) do |body|
      MultiJson.load(body)
    end

    should_respond_to(:yaml) do |body|
      YAML.load(body)
    end

    should "return Last-Modified header" do
      get_show(@rubygem)
      assert_equal @response.headers['Last-Modified'], @rubygem.updated_at.httpdate
    end

    should "return 304 when If-Modified-Since header is satisfied" do
      get_show(@rubygem)
      assert_response :success

      set_cache_header
      get_show(@rubygem)
      assert_response :not_modified
    end

    should "return 200 when If-Modified-Since header is not satisfied" do
      get_show(@rubygem)
      assert_response :success

      set_cache_header
      @rubygem.touch

      get_show(@rubygem)
      assert_response :success
    end
  end

  context "on GET to show for an unknown gem" do
    setup do
      get :show, :id => "nonexistent_gem", :format => "json"
{'fraction_non_alphanumeric': 0.12658227848101267, 'fraction_numerical': 0.016371308016877636, 'mean_word_length': 3.020352781546811, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2885200', 'n_tokens_mistral': 2014, 'n_tokens_neox': 1949, 'n_words': 488}