<commit_before>require "spec_helper"

describe RETS::HTTP do
  context "HTTP authentication" do
    it "parses the digest header" do
        http = RETS::HTTP.new({})

        # Some servers return with spaces after each comma, others don't
        http.save_digest('realm="Foo Bar",nonce="4e3b90a132bd197a1319bdf4fc7371bf",opaque="4805755a42ac82d4837fb50a7d3babeb",qop="auth"')
        http.instance_variable_get(:@digest_type).should == ["auth"]
        digest = http.instance_variable_get(:@digest)
        digest["realm"].should == "Foo Bar"
        digest["nonce"].should == "4e3b90a132bd197a1319bdf4fc7371bf"
        digest["opaque"].should == "4805755a42ac82d4837fb50a7d3babeb"
        digest["qop"].should == "auth"

        http.save_digest('realm="Foo Bar",nonce="7d8ca69b352016f88d7c3d8a040dc9e0",opaque="431d3681382c9550ffc0525839a37aa3",qop="auth"')
        http.instance_variable_get(:@digest_type).should == ["auth"]
        digest = http.instance_variable_get(:@digest)
        digest["realm"].should == "Foo Bar"
        digest["nonce"].should == "7d8ca69b352016f88d7c3d8a040dc9e0"
        digest["opaque"].should == "431d3681382c9550ffc0525839a37aa3"
        digest["qop"].should == "auth"
    end

    it "creates a digest header" do
      http = RETS::HTTP.new(:username => "foo", :password => "<PASSWORD>")
      http.save_digest('realm="Foo Bar",nonce="7d8ca69b352016f88d7c3d8a040dc9e0",opaque="431d3681382c9550ffc0525839a37aa3",qop="auth"')

      digest = http.create_digest("GET", "/foo/bar?a=b&c=d")
      digest.should == 'Digest username="foo", realm="Foo Bar", nonce="7d8ca69b352016f88d7c3d8a040dc9e0", uri="/foo/bar?a=b&c=d", algorithm=MD5, response="f899245806b35d375fe99ee440496ac7", opaque="431d3681382c9550ffc0525839a37aa3", qop="auth", nc=00000000, cnonce="581f450f288591672b0279e2986aaf03"'
    end

    it "creates a basic header" do
      http = RETS::HTTP.new(:username => "foo", :password => "<PASSWORD>")

      basic = http.create_basic
      basic.should == "Basic Zm9vOmJhcg=="
    end
  end

  context "authentication discovery" do
    it "finds basic auth" do
      uri = URI("http://foobar.com/login/login.bar")

      # The initial response while it's figuring out what authentication is
      header_mock = mock("Header")
      header_mock.stub(:get_fields).with("www-authenticate").and_return(['Basic realm="Foo Bar"'])
      header_mock.stub(:[]).and_return(nil)

      res_mock = mock("Response")
      res_mock.stub(:code).and_return("401")
      res_mock.stub(:header).and_return(header_mock)
<commit_msg>Test digest against an User Agent that won't change with new gem versions<commit_after>require "spec_helper"

describe RETS::HTTP do
  context "HTTP authentication" do
    it "parses the digest header" do
        http = RETS::HTTP.new({})

        # Some servers return with spaces after each comma, others don't
        http.save_digest('realm="Foo Bar",nonce="4e3b90a132bd197a1319bdf4fc7371bf",opaque="4805755a42ac82d4837fb50a7d3babeb",qop="auth"')
        http.instance_variable_get(:@digest_type).should == ["auth"]
        digest = http.instance_variable_get(:@digest)
        digest["realm"].should == "Foo Bar"
        digest["nonce"].should == "4e3b90a132bd197a1319bdf4fc7371bf"
        digest["opaque"].should == "4805755a42ac82d4837fb50a7d3babeb"
        digest["qop"].should == "auth"

        http.save_digest('realm="Foo Bar",nonce="7d8ca69b352016f88d7c3d8a040dc9e0",opaque="431d3681382c9550ffc0525839a37aa3",qop="auth"')
        http.instance_variable_get(:@digest_type).should == ["auth"]
        digest = http.instance_variable_get(:@digest)
        digest["realm"].should == "Foo Bar"
        digest["nonce"].should == "7d8ca69b352016f88d7c3d8a040dc9e0"
        digest["opaque"].should == "431d3681382c9550ffc0525839a37aa3"
        digest["qop"].should == "auth"
    end

    it "creates a digest header" do
      http = RETS::HTTP.new(:username => "foo", :password => "<PASSWORD>", :useragent => {:name => "FooBar"})
      http.save_digest('realm="Foo Bar",nonce="7d8ca69b352016f88d7c3d8a040dc9e0",opaque="431d3681382c9550ffc0525839a37aa3",qop="auth"')

      digest = http.create_digest("GET", "/foo/bar?a=b&c=d")
      digest.should == 'Digest username="foo", realm="Foo Bar", nonce="7d8ca69b352016f88d7c3d8a040dc9e0", uri="/foo/bar?a=b&c=d", algorithm=MD5, response="c9cfe27cc343e0b18bc857529510a76d", opaque="431d3681382c9550ffc0525839a37aa3", qop="auth", nc=00000000, cnonce="8a1f541c678feb35a19c8802ffd0c173"'
    end

    it "creates a basic header" do
      http = RETS::HTTP.new(:username => "foo", :password => "<PASSWORD>")

      basic = http.create_basic
      basic.should == "Basic Zm9vOmJhcg=="
    end
  end

  context "authentication discovery" do
    it "finds basic auth" do
      uri = URI("http://foobar.com/login/login.bar")

      # The initial response while it's figuring out what authentication is
      header_mock = mock("Header")
      header_mock.stub(:get_fields).with("www-authenticate").and_return(['Basic realm="Foo Bar"'])
      header_mock.stub(:[]).and_return(nil)

      res_mock = mock("Response")
      res_mock.stub(:code).and_return("401")
      res_mock.stub(:header).and_return(header_mock)

{'fraction_non_alphanumeric': 0.14617302896604642, 'fraction_numerical': 0.11854977939765969, 'mean_word_length': 4.43125, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '6054192', 'n_tokens_mistral': 2262, 'n_tokens_neox': 1894, 'n_words': 289}