mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-11 19:04:30 +00:00
implement this.fetch (#178)
This commit is contained in:
@@ -43,6 +43,13 @@ global.fetch = (url, opts) => {
|
||||
const middlewares = [
|
||||
serve('assets'),
|
||||
|
||||
// set test cookie
|
||||
(req, res, next) => {
|
||||
res.setHeader('Set-Cookie', 'test=woohoo!; Max-Age=3600');
|
||||
next();
|
||||
},
|
||||
|
||||
// emit messages so we can capture requests
|
||||
(req, res, next) => {
|
||||
if (!pending) return next();
|
||||
|
||||
|
||||
12
test/app/routes/credentials/index.html
Normal file
12
test/app/routes/credentials/index.html
Normal file
@@ -0,0 +1,12 @@
|
||||
<h1>{{message}}</h1>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
preload({ query }) {
|
||||
console.log(`here ${this.fetch}`);
|
||||
return this.fetch(`credentials/test.json`, {
|
||||
credentials: query.creds
|
||||
}).then(r => r.json());
|
||||
}
|
||||
};
|
||||
</script>
|
||||
28
test/app/routes/credentials/test.json.js
Normal file
28
test/app/routes/credentials/test.json.js
Normal file
@@ -0,0 +1,28 @@
|
||||
export function get(req, res) {
|
||||
const cookies = req.headers.cookie
|
||||
? req.headers.cookie.split(/,\s+/).reduce((cookies, cookie) => {
|
||||
const [pair] = cookie.split('; ');
|
||||
const [name, value] = pair.split('=');
|
||||
cookies[name] = value;
|
||||
return cookies;
|
||||
}, {})
|
||||
: {};
|
||||
|
||||
if (cookies.test) {
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
|
||||
res.end(JSON.stringify({
|
||||
message: cookies.test
|
||||
}));
|
||||
} else {
|
||||
res.writeHead(403, {
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
|
||||
res.end(JSON.stringify({
|
||||
message: 'unauthorized'
|
||||
}));
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@
|
||||
<a href='redirect-from'>redirect</a>
|
||||
<a href='blog/nope'>broken link</a>
|
||||
<a href='blog/throw-an-error'>error link</a>
|
||||
<a href='credentials?creds=include'>credentials</a>
|
||||
<a rel=prefetch class='{{page === "blog" ? "selected" : ""}}' href='blog'>blog</a>
|
||||
|
||||
<div class='hydrate-test'></div>
|
||||
|
||||
@@ -533,6 +533,32 @@ function run({ mode, basepath = '' }) {
|
||||
assert.equal(title, 'Stored title');
|
||||
});
|
||||
});
|
||||
|
||||
it('sends cookies when using this.fetch with credentials: "include"', () => {
|
||||
return nightmare.goto(`${base}/credentials?creds=include`)
|
||||
.page.title()
|
||||
.then(title => {
|
||||
assert.equal(title, 'woohoo!');
|
||||
});
|
||||
});
|
||||
|
||||
it('does not send cookies when using this.fetch without credentials', () => {
|
||||
return nightmare.goto(`${base}/credentials`)
|
||||
.page.title()
|
||||
.then(title => {
|
||||
assert.equal(title, 'unauthorized');
|
||||
});
|
||||
});
|
||||
|
||||
it('delegates to fetch on the client', () => {
|
||||
return nightmare.goto(base).init()
|
||||
.click('[href="credentials?creds=include"]')
|
||||
.wait(100)
|
||||
.page.title()
|
||||
.then(title => {
|
||||
assert.equal(title, 'woohoo!');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('headers', () => {
|
||||
|
||||
Reference in New Issue
Block a user