summaryrefslogtreecommitdiffstats
path: root/dom/push/test/xpcshell/test_register_5xxCode_http2.js
blob: 8e6029c750c8aa6153b5f3e0bf9f532b0fa4d060 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");

const { PushDB, PushService, PushServiceHttp2 } = serviceExports;

var httpServer = null;

XPCOMUtils.defineLazyGetter(this, "serverPort", function() {
  return httpServer.identity.primaryPort;
});

var retries = 0;

function subscribe5xxCodeHandler(metadata, response) {
  if (retries == 0) {
    ok(true, "Subscribe 5xx code");
    do_test_finished();
    response.setHeader("Retry-After", "1");
    response.setStatusLine(metadata.httpVersion, 500, "Retry");
  } else {
    ok(true, "Subscribed");
    do_test_finished();
    response.setHeader(
      "Location",
      "http://localhost:" + serverPort + "/subscription"
    );
    response.setHeader(
      "Link",
      '</pushEndpoint>; rel="urn:ietf:params:push", ' +
        '</receiptPushEndpoint>; rel="urn:ietf:params:push:receipt"'
    );
    response.setStatusLine(metadata.httpVersion, 201, "OK");
  }
  retries++;
}

function listenSuccessHandler(metadata, response) {
  Assert.ok(true, "New listener point");
  ok(retries == 2, "Should try 2 times.");
  do_test_finished();
  response.setHeader("Retry-After", "10");
  response.setStatusLine(metadata.httpVersion, 500, "Retry");
}

httpServer = new HttpServer();
httpServer.registerPathHandler("/subscribe5xxCode", subscribe5xxCodeHandler);
httpServer.registerPathHandler("/subscription", listenSuccessHandler);
httpServer.start(-1);

function run_test() {
  do_get_profile();
  setPrefs({
    "testing.allowInsecureServerURL": true,
    "http2.retryInterval": 1000,
    "http2.maxRetries": 2,
  });

  run_next_test();
}

add_task(async function test1() {
  let db = PushServiceHttp2.newPushDB();
  registerCleanupFunction(() => {
    return db.drop().then(_ => db.close());
  });

  do_test_pending();
  do_test_pending();
  do_test_pending();
  do_test_pending();

  var serverURL = "http://localhost:" + httpServer.identity.primaryPort;

  PushService.init({
    serverURI: serverURL + "/subscribe5xxCode",
    db,
  });

  let originAttributes = ChromeUtils.originAttributesToSuffix({
    inIsolatedMozBrowser: false,
  });
  let newRecord = await PushService.register({
    scope: "https://example.com/retry5xxCode",
    originAttributes,
  });

  var subscriptionUri = serverURL + "/subscription";
  var pushEndpoint = serverURL + "/pushEndpoint";
  var pushReceiptEndpoint = serverURL + "/receiptPushEndpoint";
  equal(
    newRecord.endpoint,
    pushEndpoint,
    "Wrong push endpoint in registration record"
  );

  equal(
    newRecord.pushReceiptEndpoint,
    pushReceiptEndpoint,
    "Wrong push endpoint receipt in registration record"
  );

  let record = await db.getByKeyID(subscriptionUri);
  equal(
    record.subscriptionUri,
    subscriptionUri,
    "Wrong subscription ID in database record"
  );
  equal(
    record.pushEndpoint,
    pushEndpoint,
    "Wrong push endpoint in database record"
  );
  equal(
    record.pushReceiptEndpoint,
    pushReceiptEndpoint,
    "Wrong push endpoint receipt in database record"
  );
  equal(
    record.scope,
    "https://example.com/retry5xxCode",
    "Wrong scope in database record"
  );

  httpServer.stop(do_test_finished);
});