<commit_before>require_relative "../test_helper"

describe Committee::Middleware::ResponseValidation do
  include Rack::Test::Methods

  def app
    @app
  end

  it "passes through a valid response" do
    @app = new_rack_app(MultiJson.encode([ValidApp]))
    get "/apps"
    assert_equal 200, last_response.status
  end

  it "detects an invalid response" do
    @app = new_rack_app("")
    get "/apps"
    assert_equal 500, last_response.status
    assert_match /valid JSON/i, last_response.body
  end

  it "ignores a non-2xx invalid response" do
    @app = new_rack_app("[]", {}, app_status: 404)
    get "/apps"
    assert_equal 404, last_response.status
  end

  it "rescues JSON errors" do
    @app = new_rack_app("[{x:y}]")
    get "/apps"
    assert_equal 500, last_response.status
    assert_match /valid json/i, last_response.body
  end

  it "takes a prefix" do
    @app = new_rack_app(MultiJson.encode([ValidApp]), {}, prefix: "/v1")
    get "/v1/apps"
    assert_equal 200, last_response.status
  end

  it "warns when sending a deprecated string" do
    mock(Committee).warn_deprecated.with_any_args
    @app = new_rack_app(MultiJson.encode([ValidApp]), {},
      schema: File.read("./test/data/schema.json"))
    get "/apps"
    assert_equal 200, last_response.status
  end

  it "rescues JSON errors" do
    @app = new_rack_app("[{x:y}]", {}, raise: true)
    assert_raises(Committee::InvalidResponse) do
      get "/apps"
    end
  end<commit_msg>add test to pass through 204 (no content) response<commit_after>require_relative "../test_helper"

describe Committee::Middleware::ResponseValidation do
  include Rack::Test::Methods

  def app
    @app
  end

  it "passes through a valid response" do
    @app = new_rack_app(MultiJson.encode([ValidApp]))
    get "/apps"
    assert_equal 200, last_response.status
  end

  it "detects an invalid response" do
    @app = new_rack_app("")
    get "/apps"
    assert_equal 500, last_response.status
    assert_match /valid JSON/i, last_response.body
  end

  it "ignores a non-2xx invalid response" do
    @app = new_rack_app("[]", {}, app_status: 404)
    get "/apps"
    assert_equal 404, last_response.status
  end

  it "passes through a 204 (no content) response" do
    @app = new_rack_app("", {}, app_status: 204)
    get "/apps"
    assert_equal 204, last_response.status
  end

  it "rescues JSON errors" do
    @app = new_rack_app("[{x:y}]")
    get "/apps"
    assert_equal 500, last_response.status
    assert_match /valid json/i, last_response.body
  end

  it "takes a prefix" do
    @app = new_rack_app(MultiJson.encode([ValidApp]), {}, prefix: "/v1")
    get "/v1/apps"
    assert_equal 200, last_response.status
  end

  it "warns when sending a deprecated string" do
    mock(Committee).warn_deprecated.with_any_args
    @app = new_rack_app(MultiJson.encode([ValidApp]), {},
      schema: File.read("./test/data/schema.json"))
    get "/apps"
    assert_equal 200, last_response.status
  end

  it "rescues JSON errors" do
    @app = new_rack_app("[{x:y}]", {}, raise: true)
    assert_raises(Committee::InvalidResponse) do
      get "/apps"
    end
  end
{'fraction_non_alphanumeric': 0.1204972904048454, 'fraction_numerical': 0.01912655403251514, 'mean_word_length': 3.451063829787234, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5990853', 'n_tokens_mistral': 1123, 'n_tokens_neox': 1040, 'n_words': 280}