Fix Sentry plugin tests
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2020 Spotify AB
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { SentryApiForwarder } from './sentry-api';
|
||||
import axios from 'axios';
|
||||
|
||||
jest.mock('axios', () => ({
|
||||
default: {
|
||||
post: jest.fn(),
|
||||
},
|
||||
}));
|
||||
describe('SentryApiForwarder', () => {
|
||||
it('should generate headers based on token passed in constructor', () => {
|
||||
const forwarder = new SentryApiForwarder('testtoken');
|
||||
|
||||
expect(forwarder.getRequestHeaders()).toEqual({
|
||||
headers: {
|
||||
Authorization: `Bearer testtoken`,
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -18,18 +18,23 @@ import axios from 'axios';
|
||||
|
||||
export class SentryApiForwarder {
|
||||
constructor(private token: string) {}
|
||||
|
||||
// public for testing
|
||||
public getRequestHeaders() {
|
||||
return {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.token}`,
|
||||
},
|
||||
};
|
||||
}
|
||||
public fowardRequest(request: express.Request, response: express.Response) {
|
||||
const sentryUrl = request.path;
|
||||
axios
|
||||
.get(`https://sentry.io/${sentryUrl}`, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.token}`,
|
||||
},
|
||||
})
|
||||
.then(res => {
|
||||
.get(`https://sentry.io/${sentryUrl}`, this.getRequestHeaders())
|
||||
.then((res) => {
|
||||
response.send(res.data);
|
||||
})
|
||||
.catch(err => {
|
||||
.catch((err) => {
|
||||
return response.status(err.response.status).json({
|
||||
detail: err.response.statusText,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user