# typed: false
require "rails_helper"

describe Scraper::HttpClient do
  class TestHttpClientConfig < Scraper::HttpClientConfig
    def cookies
      []
    end

    def allowed_domains
      %w[*.example.com furarchiver.net]
    end

    def ratelimit
      [["*", :none]]
    end

    def redirect_limit
      1
    end
  end

  it "throws on unallowed domain" do
    client =
      Scraper::HttpClient.new(
        TestHttpClientConfig.new,
        SpecUtil.mock_http_performer(:get, "https://example.com"),
      )
    assert_raises(Scraper::HttpClient::InvalidURLError) do
      client.get("https://foobar.com")
    end
  end

  it "creates an http log entry" do
    client =
      Scraper::HttpClient.new(
        TestHttpClientConfig.new,
        SpecUtil.mock_http_performer(
          :get,
          "https://www.example.com/",
          request_headers: {
            "Cookie" => "",
          },
          response_code: 200,
          response_time_ms: 15,
          response_headers: {
            "content-type" => "text/plain",
          },
          response_body: "the response " + SpecUtil.random_string(16),
        ),
      )
    client.logger.level = :error

    # note the lack of trailing slash - http client should set path to '/'
    response = client.get("https://www.example.com")

    assert_equal(200, response.status_code)
    assert_match(/the response /, response.body)

    log_entry = response.log_entry
    assert(log_entry.persisted?)
    assert_equal("text/plain", log_entry.content_type)
    assert_in_delta(Time.now, log_entry.requested_at, 50)
    assert_equal(15, log_entry.response_time_ms)
    assert_equal("get", log_entry.verb)
    assert_equal(200, log_entry.status_code)
    assert_equal("text/plain", log_entry.response.content_type)
    assert_match(/the response/, log_entry.response.content_bytes)
  end

  it "fixes buggy fur archiver 404 response (xml not found)" do
    client =
      Scraper::HttpClient.new(
        TestHttpClientConfig.new,
        SpecUtil.mock_http_performer(
          :get,
          "https://furarchiver.net/File/View?artist=akirameerkat&filename=1165272531.akirameerkat_meerkat_feet_1.jpg",
          request_headers: {
            "Cookie" => "",
          },
          response_code: 200,
          response_headers: {
            "content-type" => "image/jpeg",
          },
          response_body:
            File.binread(
              Rails.root.join(
                "test/fixtures/files/1165272531.akirameerkat_meerkat_feet_1.jpg",
              ),
            ),
        ),
      )

    response =
      client.get(
        "https://furarchiver.net/File/View?artist=akirameerkat&filename=1165272531.akirameerkat_meerkat_feet_1.jpg",
      )
    expect(response.status_code).to eq(404)
    expect(response.body).to include("Error 404 - File Not Found")
    expect(response.log_entry.status_code).to eq(404)
    expect(response.log_entry.content_type).to eq("text/xml")
  end

  it "fixes buggy fur archiver 404 response (image not found gif)" do
    client =
      Scraper::HttpClient.new(
        TestHttpClientConfig.new,
        SpecUtil.mock_http_performer(
          :get,
          "https://furarchiver.net/File/View?artist=summercat&filename=1274587736.summercat_koreal.jpg",
          request_headers: {
            "Cookie" => "",
          },
          response_code: 200,
          response_headers: {
            "content-type" => "image/jpeg",
          },
          response_body:
            File.binread(
              Rails.root.join(
                "test/fixtures/files/1274587736.summercat_koreal.jpg",
              ),
            ),
        ),
      )

    response =
      client.get(
        "https://furarchiver.net/File/View?artist=summercat&filename=1274587736.summercat_koreal.jpg",
      )
    expect(response.status_code).to eq(404)
    expect(response.log_entry.status_code).to eq(404)
    expect(response.log_entry.content_type).to eq("image/gif")
    expect(HexUtil.bin2hex(response.log_entry.response.sha256)).to eq(
      "fbe9b8727e0ae24baacf63b6553d331c84779e40b743380628a5181e0e9fa2ff",
    )
  end
end
