1#!/usr/bin/env perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 12;
7
8use_ok 'Protocol::WebSocket::Response';
9
10my $res;
11my $message;
12
13$res = Protocol::WebSocket::Response->new;
14$res->version('draft-hixie-75');
15$res->host('example.com');
16is $res->to_string => "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a"
17  . "Upgrade: WebSocket\x0d\x0a"
18  . "Connection: Upgrade\x0d\x0a"
19  . "WebSocket-Origin: http://example.com\x0d\x0a"
20  . "WebSocket-Location: ws://example.com/\x0d\x0a"
21  . "\x0d\x0a";
22
23$res = Protocol::WebSocket::Response->new;
24$res->version('draft-hixie-75');
25$res->host('example.com');
26$res->subprotocol('sample');
27is $res->to_string => "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a"
28  . "Upgrade: WebSocket\x0d\x0a"
29  . "Connection: Upgrade\x0d\x0a"
30  . "WebSocket-Protocol: sample\x0d\x0a"
31  . "WebSocket-Origin: http://example.com\x0d\x0a"
32  . "WebSocket-Location: ws://example.com/\x0d\x0a"
33  . "\x0d\x0a";
34
35$res = Protocol::WebSocket::Response->new;
36$res->version('draft-hixie-75');
37$res->host('example.com');
38$res->secure(1);
39is $res->to_string => "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a"
40  . "Upgrade: WebSocket\x0d\x0a"
41  . "Connection: Upgrade\x0d\x0a"
42  . "WebSocket-Origin: https://example.com\x0d\x0a"
43  . "WebSocket-Location: wss://example.com/\x0d\x0a"
44  . "\x0d\x0a";
45
46$res = Protocol::WebSocket::Response->new;
47$res->version('draft-hixie-75');
48$res->host('example.com');
49$res->resource_name('/demo');
50$res->origin('file://');
51$res->cookie(name => 'foo', value => 'bar', path => '/');
52
53is $res->to_string => "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a"
54  . "Upgrade: WebSocket\x0d\x0a"
55  . "Connection: Upgrade\x0d\x0a"
56  . "WebSocket-Origin: file://\x0d\x0a"
57  . "WebSocket-Location: ws://example.com/demo\x0d\x0a"
58  . "Set-Cookie: foo=bar; Path=/; Version=1\x0d\x0a"
59  . "\x0d\x0a";
60
61
62$res = Protocol::WebSocket::Response->new;
63$res->parse("HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a");
64$res->parse("Upgrade: WebSocket\x0d\x0a");
65$res->parse("Connection: Upgrade\x0d\x0a");
66$res->parse("WebSocket-Protocol: sample\x0d\x0a");
67$res->parse("WebSocket-Origin: file://\x0d\x0a");
68$res->parse("WebSocket-Location: ws://example.com/demo\x0d\x0a");
69$res->parse("\x0d\x0a\x00foo\xff");
70ok $res->is_done;
71is $res->version     => 'draft-hixie-75';
72is $res->subprotocol => 'sample';
73
74$message =
75    "HTTP/1.1 101 WebSocket Protocol Handshake\x0d\x0a"
76  . "Upgrade: WebSocket\x0d\x0a"
77  . "Connection: Upgrade\x0d\x0a"
78  . "WebSocket-Origin: file://\x0d\x0a"
79  . "WebSocket-Location: ws://example.com/demo\x0d\x0a"
80  . "\x0d\x0a\x00foo\xff";
81$res = Protocol::WebSocket::Response->new;
82ok $res->parse($message);
83ok $res->is_done;
84is $res->version => 'draft-hixie-75';
85is $message      => "\x00foo\xff";
86