<commit_before>require 'spec/helper'

#  Sample request/response are from pywebsocket spec http://code.google.com/p/pywebsocket/

GOOD_RESPONSE_SECURE = [
  'HTTP/1.1 101 WebSocket Protocol Handshake\r\n',
  'Upgrade: WebSocket\r\n',
  'Connection: Upgrade\r\n',
  'Sec-WebSocket-Location: wss://example.com/demo\r\n',
  'Sec-WebSocket-Origin: http://example.com\r\n',
  'Sec-WebSocket-Protocol: sample\r\n',
  '\r\n',
  '8jKS\'y:G*Co,Wxa-'
]

GOOD_RESPONSE_SECURE_NONDEF = [
  'HTTP/1.1 101 WebSocket Protocol Handshake\r\n',
  'Upgrade: WebSocket\r\n',
  'Connection: Upgrade\r\n',
  'Sec-WebSocket-Location: wss://example.com:8081/demo\r\n',
  'Sec-WebSocket-Origin: http://example.com\r\n',
  'Sec-WebSocket-Protocol: sample\r\n',
  '\r\n',
  '8jKS\'y:G*Co,Wxa-'
]

BAD_REQUESTS = [
    [  # HTTP request
        80,
        'GET',
        '/demo',
        {
            'Host' => 'www.google.com',
            'User-Agent' => ['Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5;',
                         ' en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3',
                         ' GTB6 GTBA'],
            'Accept' => ['text/html,application/xhtml+xml,application/xml;q=0.9,',
                     '*/*;q=0.8'],
            'Accept-Language' => 'en-us,en;q=0.5',
            'Accept-Encoding' => 'gzip,deflate',
            'Accept-Charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
            'Keep-Alive' => '300',
            'Connection' => 'keep-alive',
        }
    ],
    [  # Wrong method
        80,
        'POST',
        '/demo',
        {
            'Host' => 'example.com',
            'Connection' => 'Upgrade',
            'Sec-WebSocket-Key2' => '12998 5 Y3 1  .P00',
            'Sec-WebSocket-Protocol' => 'sample',
            'Upgrade' => 'WebSocket',
            'Sec-WebSocket-Key1' => '<KEY>',
            'Origin' => 'http://example.com',
        },
        '^n:ds[4U'
    ],
    [  # Missing Upgrade
        80,
        'GET',
        '/demo',
        {
            'Host' => 'example.com',
            'Connection' => 'Upgrade',
            'Sec-WebSocket-Key2' => '12998 5 Y3 1  .P00',
            'Sec-WebSocket-Protocol' => 'sample',
            'Sec-WebSocket-Key1' => '<KEY>',
            'Origin' => 'http://example.com',
        },
        '^n:ds[4U'
    ],
    [  # Wrong Upgrade
        80,
        'GET',
        '/demo',
        {
            'Host' => 'example.com',
            'Connection' => 'Upgrade',
            'Sec-WebSocket-Key2' => '12998 5 Y3 1  .P00',
            'Sec-WebSocket-Protocol' => 'sample',
            'Upgrade' => 'NonWebSocket',
            'Sec-WebSocket-Key1' => '<KEY>',
            'Origin' => 'http://example.com',
        },
        '^n:ds[4U'
    ],
    [  # Empty WebSocket-Protocol
        80,
        'GET',
        '/demo',
        {
            'Host' => 'example.com',
            'Connection' => 'Upgrade',
            'Sec-WebSocket-Key2' => '12998 5 Y3 1  .P00',
            'Sec-WebSocket-Protocol' => '',
            'Upgrade' => 'WebSocket',
            'Sec-WebSocket-Key1' => '<KEY>',
            'Origin' => 'http://example.com',
        },
        '^n:ds[4U'
    ],
    [  # Wrong port number format
        80,
        'GET',
        '/demo',
        {
            'Host' => 'example.com:0x50',
            'Connection' => 'Upgrade',
            'Sec-WebSocket-Key2' => '12998 5 Y3 1  .P00',
            'Sec-WebSocket-Protocol' => 'sample',
            'Upgrade' => 'WebSocket',
            'Sec-WebSocket-Key1' => '<KEY>',
            'Origin' => 'http://example.com',
        },
        '^n:ds[4U'
    ],
    [  # Header/connection port mismatch
        8080,
        'GET',
        '/demo',
        {
            'Host' => 'example.com',
            'Connection' => 'Upgrade',
            'Sec-WebSocket-Key2' => '12998 5 Y3 1  .P00',
            'Sec-WebSocket-Protocol' => 'sample',
            'Upgrade' => 'WebSocket',
            'Sec-WebSocket-Key1' => '<KEY>',
            'Origin' => 'http://example.com',
        },
        '^n:ds[4U'
    ],
    [  # Illegal WebSocket-Protocol
        80,
        'GET',
        '/demo',
        {
            'Host' => 'example.com',
            'Connection' => 'Upgrade',
            'Sec-WebSocket-Key2' => '12998 5 Y3 1  .P00',
            'Sec-WebSocket-Protocol' => 'illegal\x09protocol',
            'Upgrade' => 'WebSocket',
            'Sec-WebSocket-Key1' => '<KEY>',
            'Origin' => 'http://example.com',
        },
        '^n:ds[4U'
    ],
]

describe "EventMachine::WebSocket::Handler" do
  def format_request(r)
    data = "#{r[:method]} #{r[:path]} HTTP/1.1\r\n"
    header_lines = r[:headers].map { |k,v| "#{k}: #{v}" }
    data << [header_lines, '', r[:body]].join("\r\n")
    data
  end

  def format_response(r)
    data = "HTTP/1.1 101 WebSocket Protocol Handshake\r\n"
    header_lines = r[:headers].map { |k,v| "#{k}: #{v}" }
    data << [header_lines, '', r[:body]].join("\r\n")
    data
  end

  def handler(request)
    EM::WebSocket::HandlerFactory.build(format_request(request))
  end
  
  def send_handshake(response)<commit_msg>Remove pywebsocket template tests for draft 76<commit_after>require 'spec/helper'

describe "EventMachine::WebSocket::Handler" do
  def format_request(r)
    data = "#{r[:method]} #{r[:path]} HTTP/1.1\r\n"
    header_lines = r[:headers].map { |k,v| "#{k}: #{v}" }
    data << [header_lines, '', r[:body]].join("\r\n")
    data
  end

  def format_response(r)
    data = "HTTP/1.1 101 WebSocket Protocol Handshake\r\n"
    header_lines = r[:headers].map { |k,v| "#{k}: #{v}" }
    data << [header_lines, '', r[:body]].join("\r\n")
    data
  end

  def handler(request)
    EM::WebSocket::HandlerFactory.build(format_request(request))
  end
  
  def send_handshake(response)
{'fraction_non_alphanumeric': 0.19553555978543, 'fraction_numerical': 0.03218549922131857, 'mean_word_length': 1.8208882381649585, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 2}, 'pii_count': 1, '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': '1436417', 'n_tokens_mistral': 2143, 'n_tokens_neox': 2036, 'n_words': 342}