<commit_before>require 'support/test_app'
require 'net/http'

describe Mizuno::Server do
  include HttpRequests

  describe 'http' do
    before(:all) do
      start_server(TestApp.new, host: '127.0.0.1', port: 9201,
                                embedded: true, threads: 10)
    end

    after(:all) do
      stop_server
    end

    it '200 OK' do
      response = get('/ping')
      expect(response.code).to eq '200'
      expect(response.body).to eq 'OK'
    end

    it '403 FORBIDDEN' do
      response = get('/error/403')
      expect(response.code).to eq '403'
    end

    it '404 NOT FOUND' do
      response = get('/jimmy/hoffa')
      expect(response.code).to eq '404'
    end

    it 'rack headers' do
      response = get('/echo')
      expect(response.code).to eq '200'

      content = JSON.parse(response.body)
      expect(content['rack.version']).to eq [1, 2]
      expect(content['rack.multithread']).to be_true
      expect(content['rack.multiprocess']).to be_false
      expect(content['rack.run_once']).to be_false
    end

    it 'form variables via GET' do
      response = get('/echo?answer=42')
      expect(response.code).to eq '200'

      content = JSON.parse(response.body)
      expect(content['request.params']['answer']).to eq '42'
    end

    it 'form variables via POST' do
      question = 'What is the answer to life?'
      response = post('/echo', 'question' => question)
      expect(response.code).to eq '200'

      content = JSON.parse(response.body)
      expect(content['request.params']['question']).to eq question
    end

    it 'custom headers' do
      response = get('/echo', 'X-My-Header' => 'Pancakes')
      expect(response.code).to eq '200'

      content = JSON.parse(response.body)
      expect(content['HTTP_X_MY_HEADER']).to eq 'Pancakes'
    end

    it 'rack.java.servlet' do
      response = get('/echo', 'answer' => '42')
      expect(response.code).to eq '200'

      content = JSON.parse(response.body)
      expect(content['rack.java.servlet']).to be_true
    end

    it 'hides server version' do
      response = get('/ping')
      expect(response['server']).to be_nil
    end

    it 'server port and hostname' do
      response = get('/echo')
      content = JSON.parse(response.body)
      expect(content['SERVER_PORT']).to eq '9201'
      expect(content['SERVER_NAME']).to eq '127.0.0.1'
    end

    it 'uri scheme' do
      response = get('/echo')
      content = JSON.parse(response.body)
      expect(content['rack.url_scheme']).to eq 'http'
    end

    it 'file downloads' do
      response = get('/download')<commit_msg>Fixed server specs to work again (#10)<commit_after>require 'net/http'
require 'openssl'

require 'support/test_app'

describe Mizuno::Server do
  include HttpRequests

  describe 'http' do
    before(:all) do
      start_server(TestApp.new, host: '127.0.0.1', port: 9201,
                                embedded: true, threads: 10)
    end

    after(:all) do
      stop_server
    end

    it '200 OK' do
      response = get('/ping')
      expect(response.code).to eq '200'
      expect(response.body).to eq 'OK'
    end

    it '403 FORBIDDEN' do
      response = get('/error/403')
      expect(response.code).to eq '403'
    end

    it '404 NOT FOUND' do
      response = get('/jimmy/hoffa')
      expect(response.code).to eq '404'
    end

    it 'rack headers' do
      response = get('/echo')
      expect(response.code).to eq '200'

      content = JSON.parse(response.body)
      expect(content['rack.version']).to eq [1, 3]
      expect(content['rack.multithread']).to be true
      expect(content['rack.multiprocess']).to be false
      expect(content['rack.run_once']).to be false
    end

    it 'form variables via GET' do
      response = get('/echo?answer=42')
      expect(response.code).to eq '200'

      content = JSON.parse(response.body)
      expect(content['request.params']['answer']).to eq '42'
    end

    it 'form variables via POST' do
      question = 'What is the answer to life?'
      response = post('/echo', 'question' => question)
      expect(response.code).to eq '200'

      content = JSON.parse(response.body)
      expect(content['request.params']['question']).to eq question
    end

    it 'custom headers' do
      response = get('/echo', 'X-My-Header' => 'Pancakes')
      expect(response.code).to eq '200'

      content = JSON.parse(response.body)
      expect(content['HTTP_X_MY_HEADER']).to eq 'Pancakes'
    end

    it 'rack.java.servlet' do
      response = get('/echo', 'answer' => '42')
      expect(response.code).to eq '200'

      content = JSON.parse(response.body)
      expect(content['rack.java.servlet']).to be true
    end

    it 'hides server version' do
      response = get('/ping')
      expect(response['server']).to be_nil
    end

    it 'server port and hostname' do
      response = get('/echo')
      content = JSON.parse(response.body)
      expect(content['SERVER_PORT']).to eq '9201'
      expect(content['SERVER_NAME']).to eq '127.0.0.1'
    end

    it 'uri scheme' do
      response = get('/echo')
      content = JSON.parse(response.body)
      expect(content['rack.url_scheme']).to eq 'http'
    end

    it 'file downloads' do
      response = get('/download')
{'fraction_non_alphanumeric': 0.14114285714285715, 'fraction_numerical': 0.025523809523809525, 'mean_word_length': 2.572108843537415, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '309584', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1631, 'n_words': 419}