test_basic_functionality.js
Go to the documentation of this file.
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et: */
3 /* ***** BEGIN LICENSE BLOCK *****
4  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is httpd.js code.
17  *
18  * The Initial Developer of the Original Code is
19  * Jeff Walden <jwalden+code@mit.edu>.
20  * Portions created by the Initial Developer are Copyright (C) 2006
21  * the Initial Developer. All Rights Reserved.
22  *
23  * Contributor(s):
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38 
39 /*
40  * Basic functionality test, from the client programmer's POV.
41  */
42 
43 var tests =
44  [
45  new Test("http://localhost:4444/objHandler",
47  new Test("http://localhost:4444/functionHandler",
49  new Test("http://localhost:4444/non-existent-path",
51  ];
52 
53 function run_test()
54 {
55  var srv = createServer();
56 
57  // base path
58  // XXX should actually test this works with a file by comparing streams!
59  var dirServ = Cc["@mozilla.org/file/directory_service;1"]
60  .getService(Ci.nsIProperties);
61  var path = dirServ.get("CurProcD", Ci.nsILocalFile);
62  srv.registerDirectory("/", path);
63 
64  // register a few test paths
65  srv.registerPathHandler("/objHandler", objHandler);
66  srv.registerPathHandler("/functionHandler", functionHandler);
67 
68  srv.start(4444);
69 
71 }
72 
73 
74 // TEST DATA
75 
76 // common properties *always* appended by server
77 // or invariants for every URL in paths
78 function commonCheck(ch)
79 {
80  do_check_true(ch.contentLength > -1);
81  do_check_eq(ch.getResponseHeader("connection"), "close");
82  do_check_false(ch.isNoStoreResponse());
83 }
84 
85 function start_objHandler(ch, cx)
86 {
87  commonCheck(ch);
88 
89  do_check_eq(ch.responseStatus, 200);
90  do_check_true(ch.requestSucceeded);
91  do_check_eq(ch.getResponseHeader("content-type"), "text/plain");
92  do_check_eq(ch.responseStatusText, "OK");
93 
94  var reqMin = {}, reqMaj = {}, respMin = {}, respMaj = {};
95  ch.getRequestVersion(reqMaj, reqMin);
96  ch.getResponseVersion(respMaj, respMin);
97  do_check_true(reqMaj.value == respMaj.value &&
98  reqMin.value == respMin.value);
99 }
100 
101 function start_functionHandler(ch, cx)
102 {
103  commonCheck(ch);
104 
105  do_check_eq(ch.responseStatus, 404);
106  do_check_false(ch.requestSucceeded);
107  do_check_eq(ch.getResponseHeader("foopy"), "quux-baz");
108  do_check_eq(ch.responseStatusText, "Page Not Found");
109 
110  var reqMin = {}, reqMaj = {}, respMin = {}, respMaj = {};
111  ch.getRequestVersion(reqMaj, reqMin);
112  ch.getResponseVersion(respMaj, respMin);
113  do_check_true(reqMaj.value == 1 && reqMin.value == 1);
114  do_check_true(respMaj.value == 1 && respMin.value == 1);
115 }
116 
117 function start_non_existent_path(ch, cx)
118 {
119  commonCheck(ch);
120 
121  do_check_eq(ch.responseStatus, 404);
122  do_check_false(ch.requestSucceeded);
123 }
124 
125 
126 // PATH HANDLERS
127 
128 // /objHandler
130  {
131  handle: function(metadata, response)
132  {
133  response.setStatusLine(metadata.httpVersion, 200, "OK");
134  response.setHeader("Content-Type", "text/plain", false);
135 
136  var body = "Request (slightly reformatted):\n\n";
137  body += metadata.method + " " + metadata.path;
138 
139  do_check_eq(metadata.port, 4444);
140 
141  if (metadata.queryString)
142  body += "?" + metadata.queryString;
143 
144  body += " HTTP/" + metadata.httpVersion + "\n";
145 
146  var headEnum = metadata.headers;
147  while (headEnum.hasMoreElements())
148  {
149  var fieldName = headEnum.getNext()
150  .QueryInterface(Ci.nsISupportsString)
151  .data;
152  body += fieldName + ": " + metadata.getHeader(fieldName) + "\n";
153  }
154 
155  response.bodyOutputStream.write(body, body.length);
156  },
157  QueryInterface: function(id)
158  {
159  if (id.equals(Ci.nsISupports) || id.equals(Ci.nsIHttpRequestHandler))
160  return this;
161  throw Cr.NS_ERROR_NOINTERFACE;
162  }
163  };
164 
165 // /functionHandler
166 function functionHandler(metadata, response)
167 {
168  response.setStatusLine("1.1", 404, "Page Not Found");
169  response.setHeader("foopy", "quux-baz", false);
170 
171  do_check_eq(metadata.port, 4444);
172  do_check_eq(metadata.host, "localhost");
173  do_check_eq(metadata.path.charAt(0), "/");
174 
175  var body = "this is text\n";
176  response.bodyOutputStream.write(body, body.length);
177 }
const Cc
do_check_eq(typeof PlacesUtils,"object")
menuItem id
Definition: FeedWriter.js:971
function runHttpTests(testArray, done)
Definition: head_utils.js:340
sbOSDControlService prototype QueryInterface
function start_objHandler(ch, cx)
function handle(request, response)
function run_test()
function createServer()
Definition: head_utils.js:53
function functionHandler(metadata, response)
function commonCheck(ch)
function start_non_existent_path(ch, cx)
function start_functionHandler(ch, cx)
return null
Definition: FeedWriter.js:1143
function testComplete(srv)
Definition: head_utils.js:292
function Test(path, initChannel, onStartRequest, onStopRequest)
Definition: head_utils.js:322
const Cr
const Ci
var srv