<commit_before>require 'spec_helper'
require 'playback/request'
require 'json'

describe Playback::Request do

  def create_file(path, lines)
    File.open path, 'w' do |file|
      lines.each do |line|
        file.puts line
      end
    end
  end

  let(:base_uri) { 'http://httpbin.org' }

  let :apache_common_log_lines do
    [
      '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "GET /get HTTP/1.1" 200 73',
      '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "POST /post?c=Jewelry+Networking HTTP/1.1" 200 118',
      '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "PUT /put?c=Books HTTP/1.1" 200 53',
      '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "DELETE /delete HTTP/1.1" 200 53',
      '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "PATCH /patch?from=0 HTTP/1.1" 200 53',
    ]
  end
  let :apache_combined_log_lines do
    [
      '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "GET /get HTTP/1.1" 200 73 "http://google.co.jp" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1"',
      '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "POST /post?c=Jewelry+Networking HTTP/1.1" 200 118 "http://google.co.jp" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1"',
      '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "PUT /put?c=Books HTTP/1.1" 200 53 "http://google.co.jp" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1"',
      '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "DELETE /delete HTTP/1.1" 200 53 "http://google.co.jp" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1"',
      '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "PATCH /patch?from=0 HTTP/1.1" 200 53 "http://google.co.jp" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1"',
    ]
  end
  let(:apache_common_log_file) { './apache_common_access.log' }
  let(:apache_combined_log_file) { './apache_combined_access.log' }

  before do
    create_file(apache_common_log_file, apache_common_log_lines)
    create_file(apache_combined_log_file, apache_combined_log_lines)
  end

  after do
    File.unlink apache_common_log_file
    File.unlink apache_combined_log_file
  end

  it 'can generate and request from apache common format log' do
    req = Playback::Request.new(base_uri, apache_common_log_file, 'common')
    res = req.run

    res.each do |r|
      expect(r.class).to eq(Net::HTTPOK)
      headers = JSON.load(r.body)['headers']
      expect(headers['Referer']).to eq('')
      expect(headers['User-Agent']).to eq(Playback::Request::DEFAULT_USER_AGENT)
    end
  end

  it 'can generate and request from apache combined format log' do
    req = Playback::Request.new(base_uri, apache_combined_log_file, 'combined')
    res = req.run

    res.each do |r|
      expect(r.class).to eq(Net::HTTPOK)
      headers = JSON.load(r.body)['headers']
      expect(headers['Referer']).to eq('http://google.co.jp')
      expect(headers['User-Agent']).to eq('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1')
    end
  end

end
<commit_msg>test case for any log formats<commit_after>require 'spec_helper'
require 'playback/request'
require 'json'

describe Playback::Request do
  let(:apache_log_playback) { Playback::Request.new('http://httpbin.org', 'apache') }
  let :apache_common_logs do
    {
      GET:    '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "GET /get HTTP/1.1" 200 73',
      POST:   '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "POST /post?c=Jewelry+Networking HTTP/1.1" 200 118',
      PUT:    '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "PUT /put?c=Books HTTP/1.1" 200 53',
      DELETE: '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "DELETE /delete HTTP/1.1" 200 53',
      PATCH:  '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "PATCH /patch?from=0 HTTP/1.1" 200 53',
    }
  end
  let :apache_combined_logs do
    {
      GET:    '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "GET /get HTTP/1.1" 200 73 "http://google.co.jp" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1"',
      POST:   '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "POST /post?c=Jewelry+Networking HTTP/1.1" 200 118 "http://google.co.jp" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1"',
      PUT:    '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "PUT /put?c=Books HTTP/1.1" 200 53 "http://google.co.jp" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1"',
      DELETE: '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "DELETE /delete HTTP/1.1" 200 53 "http://google.co.jp" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1"',
      PATCH:  '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "PATCH /patch?from=0 HTTP/1.1" 200 53 "http://google.co.jp" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1"',
    }
  end
  let :apache_custom_logs do
    {
      GET:    '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "GET /get HTTP/1.1" 200 73 "http://google.co.jp" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1" "example.com" "192.168.0.1201102091208001" "901"',
      POST:   '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "POST /post?c=Jewelry+Networking HTTP/1.1" 200 118 "http://google.co.jp" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1" "example.com" "192.168.0.1201102091208001" "901"',
      PUT:    '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "PUT /put?c=Books HTTP/1.1" 200 53 "http://google.co.jp" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1" "example.com" "192.168.0.1201102091208001" "901"',
      DELETE: '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "DELETE /delete HTTP/1.1" 200 53 "http://google.co.jp" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1" virtualhost.example.jp "192.0.2.16794832933550" "09011112222333_xx.ezweb.ne.jp" 533593',
      PATCH:  '127.0.0.1 - - [07/Jun/2014:14:58:55 +0900] "PATCH /patch?from=0 HTTP/1.1" 200 53 "http://google.co.jp" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1" virtualhost.example.jp "192.0.2.16794832933550" "09011112222333_xx.ezweb.ne.jp" 533593',
    }
  end

  it 'can get request from apache common format log' do
    res = apache_log_playback.run(apache_common_logs[:GET])
    expect(res).to eq('{"method":"GET","path":"/get","status":200}')
  end

  it 'can get request from apache combined format log' do
    res = apache_log_playback.run(apache_combined_logs[:GET])
    expect(res).to eq('{"method":"GET","path":"/get","status":200}')
  end

  it 'can get request from apache customized format log' do
    res = apache_log_playback.run(apache_custom_logs[:GET])
    expect(res).to eq('{"method":"GET","path":"/get","status":200}')
  end

  it 'can post request from apache common format log' do
    res = apache_log_playback.run(apache_common_logs[:POST])
    expect(res).to eq('{"method":"POST","path":"/post?c=Jewelry+Networking","status":200}')
  end

  it 'can post request from apache combined format log' do
    res = apache_log_playback.run(apache_combined_logs[:POST])
    expect(res).to eq('{"method":"POST","path":"/post?c=Jewelry+Networking","status":200}')
  end

  it 'can post request from apache customized format log' do
    res = apache_log_playback.run(apache_custom_logs[:POST])
    expect(res).to eq('{"method":"POST","path":"/post?c=Jewelry+Networking","status":200}')
  end

  it 'can put request from apache common format log' do
    res = apache_log_playback.run(apache_common_logs[:PUT])
    expect(res).to eq('{"method":"PUT","path":"/put?c=Books","status":200}')
  end

  it 'can put request from apache combined format log' do
    res = apache_log_playback.run(apache_combined_logs[:PUT])
    expect(res).to eq('{"method":"PUT","path":"/put?c=Books","status":200}')
  end

  it 'can put request from apache customized format log' do
    res = apache_log_playback.run(apache_custom_logs[:PUT])
    expect(res).to eq('{"method":"PUT","path":"/put?c=Books","status":200}')
  end

  it 'can delete request from apache common format log' do
    res = apache_log_playback.run(apache_common_logs[:DELETE])
    expect(res).to eq('{"method":"DELETE","path":"/delete","status":200}')
  end

  it 'can delete request from apache combined format log' do
    res = apache_log_playback.run(apache_combined_logs[:DELETE])
    expect(res).to eq('{"method":"DELETE","path":"/delete","status":200}')
  end

  it 'can delete request from apache customized format log' do
    res = apache_log_playback.run(apache_custom_logs[:DELETE])
    expect(res).to eq('{"method":"DELETE","path":"/delete","status":200}')
  end

  it 'can patch request from apache common format log' do
    res = apache_log_playback.run(apache_common_logs[:PATCH])
    expect(res).to eq('{"method":"PATCH","path":"/patch?from=0","status":200}')
  end

  it 'can patch request from apache combined format log' do
    res = apache_log_playback.run(apache_combined_logs[:PATCH])
    expect(res).to eq('{"method":"PATCH","path":"/patch?from=0","status":200}')
  end

  it 'can patch request from apache customized format log' do
    res = apache_log_playback.run(apache_custom_logs[:PATCH])
    expect(res).to eq('{"method":"PATCH","path":"/patch?from=0","status":200}')
  end

end

{'fraction_non_alphanumeric': 0.18545994065281898, 'fraction_numerical': 0.13172954641797371, 'mean_word_length': 3.6419085095917363, 'pattern_counts': {'":': 45, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 30, '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': '6789292', 'n_tokens_mistral': 4619, 'n_tokens_neox': 3811, 'n_words': 775}