Skip to content
This repository has been archived by the owner on Feb 25, 2022. It is now read-only.

Commit

Permalink
fix: respect github token
Browse files Browse the repository at this point in the history
fixes #234
  • Loading branch information
tripodsan committed Apr 15, 2021
1 parent b311f66 commit fc2a8b4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,22 @@ async function main(req) {
const repo = searchParams.get('repo');
const ref = searchParams.get('ref');
const path = searchParams.get('path');
const githubToken = req.headers.get('x-github-token');
if (!(owner && repo && ref)) {
return new Response('missing owner, repo, or ref', {
status: 400,
});
}

const opts = {};
if (githubToken) {
opts.headers = {
authorization: `token ${githubToken}`,
};
}

const config = await new RedirectConfig()
.withRepo(owner, repo, ref)
.withRepo(owner, repo, ref, opts)
.init();

const match = await config.match(path);
Expand Down
46 changes: 46 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ redirects:
assert.equal(result.statusCode, 204);
});

it('204 for non existing redirect.yaml', async function test() {
const { server } = this.polly;

server
.get('https://raw.githubusercontent.com/adobe/theblog/branch2/helix-redirects.yaml')
.intercept((req, res) => {
res.status(404).send();
});

const result = await index({
owner: 'adobe',
repo: 'theblog',
ref: 'branch2',
});
assert.equal(result.statusCode, 204);
});

it('204 when non-matching path provided', async function test() {
const { server } = this.polly;

Expand All @@ -101,6 +118,35 @@ redirects:
assert.equal(result.statusCode, 204);
});

it('301 for PHP on private repo', async function test() {
const { server } = this.polly;

server
.get('https://raw.githubusercontent.com/adobe/theblog/branch3p/helix-redirects.yaml')
.intercept((req, res) => {
if (req.headers.authorization !== 'token foobar') {
res.status(404).send();
return;
}
res.status(200).send(`
redirects:
- from: (.*).php
to: $1.html
`);
});

const result = await index({
owner: 'adobe',
repo: 'theblog',
ref: 'branch3p',
path: '/test.php',
__ow_headers: {
'x-github-token': 'foobar',
},
});
assert.equal(result.statusCode, 301);
});

it('301 for PHP', async function test() {
const { server } = this.polly;

Expand Down

0 comments on commit fc2a8b4

Please sign in to comment.