Skip to content

Commit

Permalink
test: add redirect tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Coobaha committed Feb 24, 2021
1 parent 6ed4dea commit 4ef3f40
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
16 changes: 16 additions & 0 deletions test/better-fastify.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,19 @@ expectType<Service<EmptyResponses>>(s);
expectType<AsyncEmptyHandler['AsRoute']>(async (req, reply) => {
return reply.status(204);
});

interface Redirects extends Schema {
paths: {
'POST /redirect': {
response: {
302: {};
};
};
};
}

expectType<Service<Redirects>>({
'POST /redirect': (req, reply) => {
return reply.status(302).send();
},
});
38 changes: 36 additions & 2 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,23 @@ const defaultService: Service<TestSchema> = {
const fastifyReply = reply.status(204);
return fastifyReply.send();
},
'POST /redirect': async (req, reply) => {
return reply.redirect('example.com').send();
},
};

const buildApp = async (t: Test, service?: Service<TestSchema>) => {
const app = fastify();
const opts = {
logger: {
level: 'error',
serializers: {
err: (err: any) => {
t.fail('should not happen', err);
return err;
},
},
},
};
const app = fastify(opts);

service ??= defaultService;

Expand Down Expand Up @@ -175,3 +188,24 @@ t.test('POST / type casts payload when possible', async (t) => {

t.matchSnapshot(res.json(), '123 was casted to string');
});

t.test('POST /redirect works', async (t) => {
const app = await buildApp(t);
const res = await app.inject({
url: '/redirect',
method: 'POST',
});

t.equal(res.statusCode, 302);
t.equal(res.headers.location, 'example.com');
});

t.test('GET /empty works', async (t) => {
const app = await buildApp(t);
const res = await app.inject({
url: '/empty',
method: 'GET',
});

t.equal(res.statusCode, 204);
});
10 changes: 9 additions & 1 deletion test/test_schema.gen.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,15 @@
}
}
}
},
"GET /empty": {
"request": {},
"response": {}
},
"POST /redirect": {
"request": {},
"response": {}
}
},
"$hash": "e8c54bf22e2f972ebf0aae46914bb9bed6d83e6e8f1a7de1ea1b18be56ab721b__v0.0.4"
"$hash": "578359f04be3caf819bd8c91ebc19ffaebe40d23be14f598d7098b26efd29bf7__v0.2.1"
}
5 changes: 5 additions & 0 deletions test/test_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,10 @@ export interface TestSchema extends Schema {
204: {};
};
};
'POST /redirect': {
response: {
302: {};
};
};
};
}

0 comments on commit 4ef3f40

Please sign in to comment.