<commit_before>require 'spec_helper'
require 'json'

describe Http do
  let(:test_endpoint)  { "http://127.0.0.1:#{ExampleService::PORT}/" }
  let(:proxy_endpoint) { "#{test_endpoint}proxy" }

  context "getting resources" do
    it "should be easy" do
      response = Http.get test_endpoint
      response.should match(/<!doctype html>/)
    end

    context "with_response" do
      it 'allows specifying :object' do
        res = Http.with_response(:object).get test_endpoint
        res.should be_a(Http::Response)
      end
    end

    context "with headers" do
      it "should be easy" do
        response = Http.accept(:json).get test_endpoint
        response['json'].should be_true
      end
    end

    context "with callbacks" do
      it "fires a request callback" do
        pending 'Http::Request is not yet implemented'

        request = nil
        Http.on(:request) {|r| request = r}.get test_endpoint
        request.should be_a Http::Request
      end

      it "fires a response callback" do
        response = nil
        Http.on(:response) {|r| response = r}.get test_endpoint
        response.should be_a Http::Response
      end
    end

    it "should not mess with the returned status" do
      client = Http.with_response(:object)
      res = client.get test_endpoint
      res.status.should == 200
      res = client.get "#{test_endpoint}not-found"
      res.status.should == 404
    end
  end

  context "with http proxy address and port" do
    it "should proxy the request" do
      response = Http.via("127.0.0.1", 8080).get proxy_endpoint
      response.should match(/Proxy!/)
    end
  end

  context "with http proxy address, port username and password" do
    it "should proxy the request" do
      response = Http.via("127.0.0.1", 8081, "username", "password").get proxy_endpoint
      response.should match(/Proxy!/)
    end
  end

  context "with http proxy address, port, with wrong username and password" do
    it "should proxy the request" do
      pending "fixing proxy support"

      response = Http.via("127.0.0.1", 8081, "user", "pass").get proxy_endpoint
      response.should match(/Proxy Authentication Required/)
    end
  end

  context "without proxy port" do
    it "should raise an argument error" do
      expect { Http.via("127.0.0.1") }.to raise_error ArgumentError
    end
  end

  context "posting to resources" do
    it "should be easy" do
      response = Http.post "#{test_endpoint}form", :form => {:example => 'testing-form'}
      response.should == "passed :)"
    end
  end

  context "posting with an explicit body" do
    it "should be easy" do
      response = Http.post "#{test_endpoint}body", :body => "testing-body"
      response.should == "passed :)"
    end
  end

  context "with redirects" do<commit_msg>Better wording<commit_after>require 'spec_helper'
require 'json'

describe Http do
  let(:test_endpoint)  { "http://127.0.0.1:#{ExampleService::PORT}/" }
  let(:proxy_endpoint) { "#{test_endpoint}proxy" }

  context "getting resources" do
    it "should be easy" do
      response = Http.get test_endpoint
      response.should match(/<!doctype html>/)
    end

    context "with_response" do
      it 'allows specifying :object' do
        res = Http.with_response(:object).get test_endpoint
        res.should be_a(Http::Response)
      end
    end

    context "with headers" do
      it "should be easy" do
        response = Http.accept(:json).get test_endpoint
        response['json'].should be_true
      end
    end

    context "with callbacks" do
      it "fires a request callback" do
        pending 'Http::Request is not yet implemented'

        request = nil
        Http.on(:request) {|r| request = r}.get test_endpoint
        request.should be_a Http::Request
      end

      it "fires a response callback" do
        response = nil
        Http.on(:response) {|r| response = r}.get test_endpoint
        response.should be_a Http::Response
      end
    end

    it "should not mess with the returned status" do
      client = Http.with_response(:object)
      res = client.get test_endpoint
      res.status.should == 200
      res = client.get "#{test_endpoint}not-found"
      res.status.should == 404
    end
  end

  context "with http proxy address and port" do
    it "should proxy the request" do
      response = Http.via("127.0.0.1", 8080).get proxy_endpoint
      response.should match(/Proxy!/)
    end
  end

  context "with http proxy address, port username and password" do
    it "should proxy the request" do
      response = Http.via("127.0.0.1", 8081, "username", "password").get proxy_endpoint
      response.should match(/Proxy!/)
    end
  end

  context "with http proxy address, port, with wrong username and password" do
    it "should proxy the request" do
      pending "fixing proxy support"

      response = Http.via("127.0.0.1", 8081, "user", "pass").get proxy_endpoint
      response.should match(/Proxy Authentication Required/)
    end
  end

  context "without proxy port" do
    it "should raise an argument error" do
      expect { Http.via("127.0.0.1") }.to raise_error ArgumentError
    end
  end

  context "posting to resources" do
    it "should be easy to post forms" do
      response = Http.post "#{test_endpoint}form", :form => {:example => 'testing-form'}
      response.should == "passed :)"
    end
  end

  context "posting with an explicit body" do
    it "should be easy to post" do
      response = Http.post "#{test_endpoint}body", :body => "testing-body"
      response.should == "passed :)"
    end
  end

  context "with redirects" do
{'fraction_non_alphanumeric': 0.1056954115336547, 'fraction_numerical': 0.01713979646491698, 'mean_word_length': 2.6976897689768977, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 10, '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': '1500790', 'n_tokens_mistral': 1727, 'n_tokens_neox': 1622, 'n_words': 549}