summaryrefslogtreecommitdiff
path: root/alarm/node_modules/node-forge/tests/http.html
blob: 3bdf9419132418ba98c588f3daccadaa1f8f6e92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<html>
   <head>
      <link type="text/css" rel="stylesheet" media="all" href="screen.css" />
      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
      <script type="text/javascript" src="forge/util.js"></script>
      <script type="text/javascript" src="forge/socket.js"></script>
      <script type="text/javascript" src="forge/http.js"></script>
      <script type="text/javascript" src="forge/log.js"></script>
      
      <script type="text/javascript">
      //<![CDATA[
      // logging category
      var cat = 'forge.tests.http';

      window.forge.socketPool =
      {
         ready: function()
         {
            forge.log.debug(cat, 'SocketPool ready.');
         }
      };

      swfobject.embedSWF(
         'forge/SocketPool.swf', 'socketPool', '0', '0', '9.0.0',
         false, {}, {allowscriptaccess: 'always'}, {});
      
      // local aliases
      var net = window.forge.net;
      var http = window.forge.http;
      var util = window.forge.util;

      var client;
      
      function client_init()
      {
         try
         {
            var sp = net.createSocketPool({
               flashId: 'socketPool',
               policyPort: 19945,
               msie: false
            });
            client = http.createClient({
               //url: 'http://' + window.location.host,
               socketPool: sp,
               connections: 10
            });
            
            document.getElementById('feedback').innerHTML =
               'HTTP client created';
         }
         catch(ex)
         {
            forge.log.error(cat, ex);
         }
         return false;
      }
      
      function client_cleanup()
      {
         var sp = client.socketPool;
         client.destroy();
         sp.destroy();
         document.getElementById('feedback').innerHTML =
            'HTTP client cleaned up';
         return false;
      }

      function client_send()
      {
         var request = http.createRequest({
            method: 'GET',
            path: '/'
            //body: 'echo=foo',
            //headers: [{'Content-Type': 'application/x-www-form-urlencoded'}]
         });
         
         client.send({
            request: request,
            connected: function(e)
            {
               forge.log.debug(cat, 'connected', e);
            },
            headerReady: function(e)
            {
               forge.log.debug(cat, 'header ready', e);
            },
            bodyReady: function(e)
            {
               forge.log.debug(cat, 'body ready', e);
            },
            error: function(e)
            {
               forge.log.error(cat, 'error', e);
            }
         });
         document.getElementById('feedback').innerHTML =
            'HTTP request sent';
         return false;
      }

      function client_send_10()
      {
         for(var i = 0; i < 10; ++i)
         {
            client_send();
         }
         return false;
      }

      function client_stress()
      {
         for(var i = 0; i < 10; ++i)
         {
            setTimeout(function()
            {
               for(var i = 0; i < 10; ++i)
               {
                  client_send();
               }
            }, 0);
         }
         return false;
      }
      
      function client_cookies()
      {
         var cookie =
         {
            name: 'test-cookie',
            value: 'test-value',
            maxAge: -1,
            secure: false,
            path: '/'
         };
         client.setCookie(cookie);
         forge.log.debug(cat, 'cookie', client.getCookie('test-cookie'));
      }

      function client_clear_cookies()
      {
         client.clearCookies();
      }

      function request_add_cookies()
      {
         var cookie1 =
         {
            name: 'test-cookie1',
            value: 'test-value1',
            maxAge: -1,
            secure: false,
            path: '/'
         };
         var cookie2 =
         {
            name: 'test-cookie2',
            value: 'test-value2',
            maxAge: -1,
            secure: false,
            path: '/'
         };
         var request = http.createRequest({
            method: 'GET',
            path: '/'
         });
         request.addCookie(cookie1);
         request.addCookie(cookie2);
         forge.log.debug(cat, 'request', request.toString());
      }

      function response_get_cookies()
      {
         var response = http.createResponse();
         response.appendField('Set-Cookie',
            'test-cookie1=test-value1; max-age=0; path=/; secure');
         response.appendField('Set-Cookie',
            'test-cookie2=test-value2; ' +
            'expires=Thu, 21-Aug-2008 23:47:25 GMT; path=/');
         var cookies = response.getCookies();
         forge.log.debug(cat, 'cookies', cookies);
      }
      
      //]]>
      </script>
   </head>
   <body>
      <div class="nav"><a href="index.html">Forge Tests</a> / HTTP</div>

      <div class="header">
         <h1>HTTP Test</h1>
      </div>

      <div class="content">

      <div id="socketPool">
         <p>Could not load the flash SocketPool.</p>
      </div>

      <fieldset class="section">
         <ul>
            <li>Use the controls below to test the HTTP client.</li>
            <li>You currently need a JavaScript console to view the output.</li>
         </ul>
      </fieldset>

      <fieldset class="section">
      <legend>Controls</legend>
         <button id="init" onclick="javascript:return client_init();">init</button>
         <button id="cleanup" onclick="javascript:return client_cleanup();">cleanup</button>
         <button id="send" onclick="javascript:return client_send();">send</button>
         <button id="send10" onclick="javascript:return client_send_10();">send 10</button>
         <button id="stress" onclick="javascript:return client_stress();">stress</button>
         <button id="client_cookies" onclick="javascript:return client_cookies();">cookies</button>
         <button id="clear_cookies" onclick="javascript:return client_clear_cookies();">clear cookies</button>
         <button id="add_cookies" onclick="javascript:return request_add_cookies();">add cookies</button>
         <button id="get_cookies" onclick="javascript:return response_get_cookies();">get cookies</button>
      </fieldset>

      <fieldset class="section">
      <legend>Feedback</legend>
      <p>Feedback from the flash SocketPool:</p>
      <div id="feedback">
      None
      </div>

      </div>
   </body>
</html>