summaryrefslogtreecommitdiffstats
path: root/rdf/tests/unit/test_rdfredirect.js
blob: 40b01a49a133af2aa776ca3db9af1914aab3029b (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
var Cc = Components.classes;
var Ci = Components.interfaces;
var Cu = Components.utils;
var Cr = Components.results;

Cu.import("resource://testing-common/httpd.js");

function getRDFService()
{
  return Cc["@mozilla.org/rdf/rdf-service;1"].
    getService(Ci.nsIRDFService);
}

var server1, server2;

function run_test()
{
  var samplefile = do_get_file('sample.rdf');

  server1 = new HttpServer();
  server1.registerPathHandler("/sample-xs.rdf", xsRedirect);
  server1.registerPathHandler("/sample-local.rdf", localRedirect);
  server1.registerFile('/sample.rdf', samplefile);
  server1.start(4444);

  server2 = new HttpServer();
  server2.registerFile('/sample.rdf', samplefile);
  server2.start(4445);

  do_test_pending();

  new rdfLoadObserver('http://localhost:4444/sample.rdf', true);
  new rdfLoadObserver('http://localhost:4445/sample.rdf', true);
  new rdfLoadObserver('http://localhost:4444/sample-xs.rdf', false);
  new rdfLoadObserver('http://localhost:4444/sample-local.rdf', true);
}

var gPending = 0;

function rdfLoadObserver(uri, shouldPass)
{
  this.shouldPass = shouldPass;
  this.uri = uri;

  ++gPending;

  var rdfService = getRDFService();
  this.ds = rdfService.GetDataSource(uri).
    QueryInterface(Ci.nsIRDFXMLSink);
  this.ds.addXMLSinkObserver(this);
}

rdfLoadObserver.prototype =
{
  onBeginLoad : function() { },
  onInterrupt : function() { },
  onResume : function() { },
  onEndLoad : function() {
    print("Testing results of loading " + this.uri);

    var rdfs = getRDFService();
    var res = rdfs.GetResource("urn:mozilla:sample-data");
    var arc = rdfs.GetResource("http://purl.org/dc/elements/1.1/title");
    var answer = this.ds.GetTarget(res, arc, true);
    if (answer !== null) {
      do_check_true(this.shouldPass);
      do_check_true(answer instanceof Ci.nsIRDFLiteral);
      do_check_eq(answer.Value, "Sample");
    }
    else {
      do_check_false(this.shouldPass);
    }

    gPending -= 1;

    this.ds.removeXMLSinkObserver(this);

    if (gPending == 0) {
      do_test_pending();
      server1.stop(do_test_finished);
      server2.stop(do_test_finished);
    }
  },
  onError : function() { }
}

function xsRedirect(metadata, response)
{
  response.setStatusLine(metadata.httpVersion, 301, "Moved Permanently");
  response.setHeader("Location", "http://localhost:4445/sample.rdf", false);
}

function localRedirect(metadata, response)
{
  response.setStatusLine(metadata.httpVersion, 301, "Moved Permanently");
  response.setHeader("Location", "http://localhost:4444/sample.rdf", false);
}