<commit_before>shared_examples_for "integrating with a server" do
  def headerize(hash)
    Hash[hash.map do |k, v|
      ["HTTP_#{k.to_s.upcase}", v.to_s]
    end]
  end

  before :all do
    BatchApi.config.endpoint = "/batch"
    BatchApi.config.verb = :post
  end

  before :each do
    BatchApi::Errors::Base.stub(:expose_backtrace?).and_return(false)
  end

  # these are defined in the dummy app's endpoints controller
  let(:get_headers) { {"foo" => "bar"} }
  let(:get_params) { {"other" => "value" } }

  let(:get_request) { {
    url: "/endpoint",
    method: "get",
    headers: get_headers,
    params: get_params
  } }

  let(:get_result) { {
    status: 422,
    body: {
      "result" => "GET OK",
      "params" => get_params.merge(
        BatchApi.rails? ? {
          "controller" => "endpoints",
          "action" => "get"
        } : {}
      )
    },
    headers: { "GET" => "hello" }
  } }

  let(:parameter) {
    (rand * 10000).to_i
  }

  let(:parameter_request) { {
    url: "/endpoint/capture/#{parameter}",
    method: "get"
  } }

  let(:parameter_result) { {
    body: {
      "result" => parameter.to_s
    }
  } }

  # these are defined in the dummy app's endpoints controller
  let(:post_headers) { {"foo" => "bar"} }
  let(:post_params) { {"other" => "value"} }

  let(:post_request) { {
    url: "/endpoint",
    method: "post",
    headers: post_headers,
    params: post_params
  } }

  let(:post_result) { {
    status: 203,
    body: {
      "result" => "POST OK",
      "params" => post_params.merge(
        BatchApi.rails? ? {
          "controller" => "endpoints",
          "action" => "post"
        } : {}
      )
    },
    headers: { "POST" => "guten tag" }
  } }

  let(:error_request) { {
    url: "/endpoint/error",
    method: "get"
  } }

  let(:error_response) { {
    status: 500,
    body: { "error" => { "message" => "StandardError" } }
  } }

  let(:missing_request) { {
    url: "/dont/work",
    method: "delete"
  } }

  let(:missing_response) { {
    status: 404,
    body: {}
  } }

  before :each do
    @t = Time.now
    xhr :post, "/batch", {
      ops: [
        get_request,
        post_request,
        error_request,
        missing_request,
        parameter_request
      ],
      sequential: true
    }.to_json, "CONTENT_TYPE" => "application/json"
  end

  it "returns a 200" do
    response.status.should == 200
  end

  it "includes results" do
    JSON.parse(response.body)["results"].should be_a(Array)
  end

  context "for a get request" do
    describe "the response" do
      before :each do
        @result = JSON.parse(response.body)["results"][0]
      end

      it "returns the body as objects" do
        @result = JSON.parse(response.body)["results"][0]
        @result["body"].should == get_result[:body]
      end

      it "returns the expected status" do
        @result["status"].should == get_result[:status]
      end

      it "returns the expected headers" do
        @result["headers"].should include(get_result[:headers])
      end

      it "verifies that the right headers were received" do
        @result["headers"]["REQUEST_HEADERS"].should include(
          headerize(get_headers)
        )
      end
    end
  end

  context "for a request with parameters" do
    describe "the response" do
      before :each do
        @result = JSON.parse(response.body)["results"][4]
      end

      it "properly parses the URL segment as a paramer" do
        @result["body"].should == parameter_result[:body]
      end
    end
  end

  context "for a post request" do
    describe "the response" do
      before :each do<commit_msg>Cleanup<commit_after>shared_examples_for "integrating with a server" do
  def headerize(hash)
    Hash[hash.map do |k, v|
      ["HTTP_#{k.to_s.upcase}", v.to_s]
    end]
  end

  before :all do
    BatchApi.config.endpoint = "/batch"
    BatchApi.config.verb = :post
  end

  before :each do
    BatchApi::Errors::Base.stub(:expose_backtrace?).and_return(false)
  end

  # these are defined in the dummy app's endpoints controller
  let(:get_headers) { {"foo" => "bar"} }
  let(:get_params) { {"other" => "value" } }

  let(:get_request) { {
    url: "/endpoint",
    method: "get",
    headers: get_headers,
    params: get_params
  } }

  let(:get_result) { {
    status: 422,
    body: {
      "result" => "GET OK",
      "params" => get_params.merge(
        BatchApi.rails? ? {
          "controller" => "endpoints",
          "action" => "get"
        } : {}
      )
    },
    headers: { "GET" => "hello" }
  } }

  let(:parameter) {
    (rand * 10000).to_i
  }

  # these are defined in the dummy app's endpoints controller
  let(:post_headers) { {"foo" => "bar"} }
  let(:post_params) { {"other" => "value"} }

  let(:post_request) { {
    url: "/endpoint",
    method: "post",
    headers: post_headers,
    params: post_params
  } }

  let(:post_result) { {
    status: 203,
    body: {
      "result" => "POST OK",
      "params" => post_params.merge(
        BatchApi.rails? ? {
          "controller" => "endpoints",
          "action" => "post"
        } : {}
      )
    },
    headers: { "POST" => "guten tag" }
  } }

  let(:error_request) { {
    url: "/endpoint/error",
    method: "get"
  } }

  let(:error_response) { {
    status: 500,
    body: { "error" => { "message" => "StandardError" } }
  } }

  let(:missing_request) { {
    url: "/dont/work",
    method: "delete"
  } }

  let(:missing_response) { {
    status: 404,
    body: {}
  } }

  before :each do
    @t = Time.now
    xhr :post, "/batch", {
      ops: [
        get_request,
        post_request,
        error_request,
        missing_request,
        parameter_request
      ],
      sequential: true
    }.to_json, "CONTENT_TYPE" => "application/json"
  end

  it "returns a 200" do
    response.status.should == 200
  end

  it "includes results" do
    JSON.parse(response.body)["results"].should be_a(Array)
  end

  context "for a get request" do
    describe "the response" do
      before :each do
        @result = JSON.parse(response.body)["results"][0]
      end

      it "returns the body as objects" do
        @result = JSON.parse(response.body)["results"][0]
        @result["body"].should == get_result[:body]
      end

      it "returns the expected status" do
        @result["status"].should == get_result[:status]
      end

      it "returns the expected headers" do
        @result["headers"].should include(get_result[:headers])
      end

      it "verifies that the right headers were received" do
        puts @result.inspect
        @result["headers"]["REQUEST_HEADERS"].should include(
          headerize(get_headers)
        )
      end
    end
  end

  context "for a request with parameters" do
    describe "the response" do
      before :each do
        @result = JSON.parse(response.body)["results"][4]
      end

      it "properly parses the URL segment as a paramer" do
        @result["body"].should == parameter_result[:body]
      end
    end
  end

  context "for a post request" do
    describe "the response" do
      before :each do
{'fraction_non_alphanumeric': 0.1462144666479032, 'fraction_numerical': 0.007317759639741064, 'mean_word_length': 2.3397556390977443, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 38, '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': '7539650', 'n_tokens_mistral': 2434, 'n_tokens_neox': 2316, 'n_words': 564}