<commit_before>RSpec.describe ShortCodeController, type: :request do
  let(:json_headers) { { 'CONTENT_TYPE' => 'application/json' } }

  describe 'POST /shorten' do
    context 'with valid parameters' do
      context 'when shortcode param is not present' do
        let(:params) { { url: 'http://myshorty.com' } }

        before { post '/shorten', params.to_json, json_headers }

        it { expect(last_response.status).to eq 201 }

        it 'returns a generated shortcode' do
          expect(json_body['shortcode']).not_to be_empty
        end
      end

      context 'when shortcode param is present' do
        let(:params) { { url: 'http://myshorty.com', shortcode: 'preferred_shortcode' } }

        context 'and this shortcode is available' do
          before { post '/shorten', params.to_json, json_headers }

          it { expect(last_response.status).to eq 201 }
          it { expect(json_body['shortcode']).to eq params[:shortcode] }
        end

        context 'and this shortcode is not available' do
          before do
            post '/shorten', params.to_json
            post '/shorten', params.to_json
          end

          it { expect(last_response.status).to eq 409 }
        end
      end
    end

    context 'when url is not present' do
      before { post '/shorten', {}.to_json, json_headers }

      it { expect(last_response.status).to eq 400 }
      it { expect(last_response.body).to eq 'url is not present' }
    end

    context 'when param shortcode is invalid' do
      before { post '/shorten', { url: 'http://myshorty.com', shortcode: 'invalid-code' }.to_json, json_headers }

      it { expect(last_response.status).to eq 422 }
      it { expect(last_response.body).to eq 'Invalid shortcode' }
    end
  end

  context 'GET /:shortcode' do
    let(:params) { { url: 'http://myshorty.com', shortcode: 'my_short_code' } }
    before { post '/shorten', params.to_json, json_headers }

    context 'when :shortcode is in database' do
      before { get "/#{params[:shortcode]}" }

      it { expect(last_response.status).to eq 302 }
      it { expect(last_response.header['Location']).to eq params[:url] }
    end

    context 'when :shortcode is not in database' do
      before { get "/yet_another_code" }

      it { expect(last_response.status).to eq 404 }
    end
  end

  context 'GET /:shortcode/stats' do
    let(:params) { { url: 'http://myshorty.com', shortcode: 'my_short_code' } }

    before { post '/shorten', params.to_json, json_headers }

    context 'when :shortcode is in database' do
      before { get "/#{params[:shortcode]}/stats" }

      it { expect(json_body).not_to be_nil }
    end

    context 'when :shortcode is not in database' do
      before { get "/yet_another_code/stats" }

      it { expect(last_response.status).to eq 404 }
    end
  end
end<commit_msg>added expectations for content-type<commit_after>RSpec.describe ShortCodeController, type: :request do
  let(:json_headers) { { 'CONTENT_TYPE' => 'application/json' } }

  describe 'POST /shorten' do
    context 'with valid parameters' do
      context 'when shortcode param is not present' do
        let(:params) { { url: 'http://myshorty.com' } }

        before { post '/shorten', params.to_json, json_headers }

        it { expect(last_response.status).to eq 201 }
        it { expect(last_response.header['Content-Type']).to eq 'application/json' }

        it 'returns a generated shortcode' do
          expect(json_body['shortcode']).not_to be_empty
        end
      end

      context 'when shortcode param is present' do
        let(:params) { { url: 'http://myshorty.com', shortcode: 'preferred_shortcode' } }

        context 'and this shortcode is available' do
          before { post '/shorten', params.to_json, json_headers }

          it { expect(last_response.status).to eq 201 }
          it { expect(last_response.header['Content-Type']).to eq 'application/json' }
          it { expect(json_body['shortcode']).to eq params[:shortcode] }
        end

        context 'and this shortcode is not available' do
          before do
            post '/shorten', params.to_json
            post '/shorten', params.to_json
          end

          it { expect(last_response.status).to eq 409 }
        end
      end
    end

    context 'when url is not present' do
      before { post '/shorten', {}.to_json, json_headers }

      it { expect(last_response.status).to eq 400 }
      it { expect(last_response.body).to eq 'url is not present' }
    end

    context 'when param shortcode is invalid' do
      before { post '/shorten', { url: 'http://myshorty.com', shortcode: 'invalid-code' }.to_json, json_headers }

      it { expect(last_response.status).to eq 422 }
      it { expect(last_response.body).to eq 'Invalid shortcode' }
    end
  end

  context 'GET /:shortcode' do
    let(:params) { { url: 'http://myshorty.com', shortcode: 'my_short_code' } }
    before { post '/shorten', params.to_json, json_headers }

    context 'when :shortcode is in database' do
      before { get "/#{params[:shortcode]}" }

      it { expect(last_response.status).to eq 302 }
      it { expect(last_response.header['Location']).to eq params[:url] }
    end

    context 'when :shortcode is not in database' do
      before { get "/yet_another_code" }

      it { expect(last_response.status).to eq 404 }
    end
  end

  context 'GET /:shortcode/stats' do
    let(:params) { { url: 'http://myshorty.com', shortcode: 'my_short_code' } }

    before { post '/shorten', params.to_json, json_headers }

    context 'when :shortcode is in database' do
      before { get "/#{params[:shortcode]}/stats" }

      it { expect(json_body).not_to be_nil }
      it { expect(last_response.header['Content-Type']).to eq 'application/json' }
    end

    context 'when :shortcode is not in database' do
      before { get "/yet_another_code/stats" }

      it { expect(last_response.status).to eq 404 }
    end
  end
end
{'fraction_non_alphanumeric': 0.12065782849471388, 'fraction_numerical': 0.008055042792414835, 'mean_word_length': 2.6586863106200123, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1781313', 'n_tokens_mistral': 1903, 'n_tokens_neox': 1810, 'n_words': 516}