rdf/tests/unit/test_rdfredirect.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 const Cc = Components.classes;
michael@0 2 const Ci = Components.interfaces;
michael@0 3 const Cu = Components.utils;
michael@0 4 const Cr = Components.results;
michael@0 5
michael@0 6 Cu.import("resource://testing-common/httpd.js");
michael@0 7
michael@0 8 function getRDFService()
michael@0 9 {
michael@0 10 return Cc["@mozilla.org/rdf/rdf-service;1"].
michael@0 11 getService(Ci.nsIRDFService);
michael@0 12 }
michael@0 13
michael@0 14 var server1, server2;
michael@0 15
michael@0 16 function run_test()
michael@0 17 {
michael@0 18 var samplefile = do_get_file('sample.rdf');
michael@0 19
michael@0 20 server1 = new HttpServer();
michael@0 21 server1.registerPathHandler("/sample-xs.rdf", xsRedirect);
michael@0 22 server1.registerPathHandler("/sample-local.rdf", localRedirect);
michael@0 23 server1.registerFile('/sample.rdf', samplefile);
michael@0 24 server1.start(4444);
michael@0 25
michael@0 26 server2 = new HttpServer();
michael@0 27 server2.registerFile('/sample.rdf', samplefile);
michael@0 28 server2.start(4445);
michael@0 29
michael@0 30 do_test_pending();
michael@0 31
michael@0 32 new rdfLoadObserver('http://localhost:4444/sample.rdf', true);
michael@0 33 new rdfLoadObserver('http://localhost:4445/sample.rdf', true);
michael@0 34 new rdfLoadObserver('http://localhost:4444/sample-xs.rdf', false);
michael@0 35 new rdfLoadObserver('http://localhost:4444/sample-local.rdf', true);
michael@0 36 }
michael@0 37
michael@0 38 var gPending = 0;
michael@0 39
michael@0 40 function rdfLoadObserver(uri, shouldPass)
michael@0 41 {
michael@0 42 this.shouldPass = shouldPass;
michael@0 43 this.uri = uri;
michael@0 44
michael@0 45 ++gPending;
michael@0 46
michael@0 47 var rdfService = getRDFService();
michael@0 48 this.ds = rdfService.GetDataSource(uri).
michael@0 49 QueryInterface(Ci.nsIRDFXMLSink);
michael@0 50 this.ds.addXMLSinkObserver(this);
michael@0 51 }
michael@0 52
michael@0 53 rdfLoadObserver.prototype =
michael@0 54 {
michael@0 55 onBeginLoad : function() { },
michael@0 56 onInterrupt : function() { },
michael@0 57 onResume : function() { },
michael@0 58 onEndLoad : function() {
michael@0 59 print("Testing results of loading " + this.uri);
michael@0 60
michael@0 61 var rdfs = getRDFService();
michael@0 62 var res = rdfs.GetResource("urn:mozilla:sample-data");
michael@0 63 var arc = rdfs.GetResource("http://purl.org/dc/elements/1.1/title");
michael@0 64 var answer = this.ds.GetTarget(res, arc, true);
michael@0 65 if (answer !== null) {
michael@0 66 do_check_true(this.shouldPass);
michael@0 67 do_check_true(answer instanceof Ci.nsIRDFLiteral);
michael@0 68 do_check_eq(answer.Value, "Sample");
michael@0 69 }
michael@0 70 else {
michael@0 71 do_check_false(this.shouldPass);
michael@0 72 }
michael@0 73
michael@0 74 gPending -= 1;
michael@0 75
michael@0 76 this.ds.removeXMLSinkObserver(this);
michael@0 77
michael@0 78 if (gPending == 0) {
michael@0 79 do_test_pending();
michael@0 80 server1.stop(do_test_finished);
michael@0 81 server2.stop(do_test_finished);
michael@0 82 }
michael@0 83 },
michael@0 84 onError : function() { }
michael@0 85 }
michael@0 86
michael@0 87 function xsRedirect(metadata, response)
michael@0 88 {
michael@0 89 response.setStatusLine(metadata.httpVersion, 301, "Moved Permanently");
michael@0 90 response.setHeader("Location", "http://localhost:4445/sample.rdf", false);
michael@0 91 }
michael@0 92
michael@0 93 function localRedirect(metadata, response)
michael@0 94 {
michael@0 95 response.setStatusLine(metadata.httpVersion, 301, "Moved Permanently");
michael@0 96 response.setHeader("Location", "http://localhost:4444/sample.rdf", false);
michael@0 97 }

mercurial