summaryrefslogtreecommitdiffstats
path: root/LayoutTests/http/tests/appcache/fallback.html
blob: ed641e07a139459216c91c003779532b4a2bc16b (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
<html manifest="resources/fallback.manifest">
<body>
<p>Test application cache fallback entries.</p>
<p>Should say SUCCESS:</p>
<div id=result></div>

<script>
if (window.layoutTestController) {
    layoutTestController.dumpAsText();
    layoutTestController.waitUntilDone();
}

var hadError = false;

function log(message)
{
    document.getElementById("result").innerHTML += message + "<br>";
}

function setNetworkEnabled(state)
{
    try {
        var req = new XMLHttpRequest;
        req.open("GET", "/resources/network-simulator.php?command=" + (state ? "connect" : "disconnect"), false);
        req.send("");
    } catch (ex) {
        log("Cannot access network simulator URL");
        hadError = true;
    }
}

function load(url)
{
    try {
        var req = new XMLHttpRequest();
        req.open("GET", url, false);
        req.send("");
        return req.responseText;
    } catch (ex) {
        log("FAIL: Cannot load " + url + ", ex = " + ex);
        hadError = true;
        return ""; // This value should not be expected as the responseText for a url presented to this function.
    }
}

var testURL = "/resources/network-simulator.php?path=/appcache/resources/not-in-cache.txt";
var nonexistentURL = "resources/does-not-exist";
var redirectURL = "resources/fallback-redirect.php";

function test()
{
    applicationCache.onnoupdate = function() { log("FAIL: received unexpected noupdate event") }
    applicationCache.oncached = function() { log("FAIL: received unexpected cached event") }
    
    setNetworkEnabled(true);

    if (!/not in the cache/.test(load(testURL))) {
        log("FAIL: Cannot load an URL from fallback namespace when network is enabled");
        hadError = true;
    }

    if (load(nonexistentURL) != "Hello, World!") {
        log("FAIL: Fallback resource wasn't used for a 404 response");
        hadError = true;
    }

    if (load(redirectURL) != "Hello, World!") {
        log("FAIL: Fallback resource wasn't used for a redirect to a resource with another origin");
        hadError = true;
    }

    test2();
}

function test2()
{
    var req = new XMLHttpRequest;
    req.open("GET", nonexistentURL);
    req.onerror = function() {
        log("FAIL: Fallback resource wasn't used for a 404 response (async)");
        hadError = true;
        test3();
    }
    req.onload = function() {
        if (req.responseText != "Hello, World!") {
            log("FAIL: Unexpected result for a 404 response (async)");
            hadError = true;
        }
        test3();
    }

    req.send("");
}

function test3()
{
    var req = new XMLHttpRequest;
    req.open("GET", redirectURL);
    req.onerror = function() {
        log("FAIL: Fallback resource wasn't used for a redirect to a resource with another origin (async)");
        hadError = true;
        test4();
    }
    req.onload = function() {
        if (req.responseText != "Hello, World!") {
            log("FAIL: Unexpected result for a redirect response (async)");
            hadError = true;
        }
        test4();
    }

    req.send("");
}

function test4()
{
    // Try loading a fallback resource as main one.

    applicationCache.onnoupdate = test5;

    var ifr = document.createElement("iframe");
    ifr.setAttribute("src", nonexistentURL);
    document.body.appendChild(ifr);
}

function test5()
{
    if (!/Hello, World/.test(window.frames[0].document.documentElement.innerHTML)) {
        log("FAIL: Fallback URL was not honored for main resource");
        hadError = true;
    }
    test6();
}

function test6()
{
    setNetworkEnabled(false);

    if (load(testURL) != load("resources/simple.txt")) {
        log("FAIL: Loading an URL from fallback namespace when network is disabled returned unexpected response");
        hadError = true;
    }

    log(hadError ? "FAILURE" : "SUCCESS");
    if (window.layoutTestController)
        layoutTestController.notifyDone();
}

applicationCache.onnoupdate = function() { test() }
applicationCache.oncached = function() { test() }

applicationCache.onupdateready = function() { log("FAIL: received unexpected updateready event") }
applicationCache.onerror = function() { log("FAIL: received unexpected error event") }

</script>
</body>
</html>