fix(git-gateway): unpublished entries not loaded for git-gateway(GitHub) (#2856)
This commit is contained in:
@ -41,7 +41,7 @@ export default class API extends GithubAPI {
|
||||
});
|
||||
}
|
||||
|
||||
getRequestHeaders(headers = {}) {
|
||||
requestHeaders(headers = {}) {
|
||||
return this.tokenPromise().then(jwtToken => {
|
||||
const baseHeader = {
|
||||
Authorization: `Bearer ${jwtToken}`,
|
||||
@ -53,38 +53,14 @@ export default class API extends GithubAPI {
|
||||
});
|
||||
}
|
||||
|
||||
urlFor(path, options) {
|
||||
const cacheBuster = new Date().getTime();
|
||||
const params = [`ts=${cacheBuster}`];
|
||||
if (options.params) {
|
||||
for (const key in options.params) {
|
||||
params.push(`${key}=${encodeURIComponent(options.params[key])}`);
|
||||
}
|
||||
}
|
||||
if (params.length) {
|
||||
path += `?${params.join('&')}`;
|
||||
}
|
||||
return this.api_root + path;
|
||||
handleRequestError(error, responseStatus) {
|
||||
throw new APIError(error.message || error.msg, responseStatus, 'Git Gateway');
|
||||
}
|
||||
|
||||
user() {
|
||||
return Promise.resolve(this.commitAuthor);
|
||||
}
|
||||
|
||||
request(path, options = {}, parseResponse = response => this.parseResponse(response)) {
|
||||
const url = this.urlFor(path, options);
|
||||
let responseStatus;
|
||||
return this.getRequestHeaders(options.headers || {})
|
||||
.then(headers => fetch(url, { ...options, headers }))
|
||||
.then(response => {
|
||||
responseStatus = response.status;
|
||||
return parseResponse(response);
|
||||
})
|
||||
.catch(error => {
|
||||
throw new APIError(error.message || error.msg, responseStatus, 'Git Gateway');
|
||||
});
|
||||
}
|
||||
|
||||
commit(message, changeTree) {
|
||||
const commitParams = {
|
||||
message,
|
||||
|
@ -0,0 +1,84 @@
|
||||
import API from '../GitHubAPI';
|
||||
|
||||
describe('github API', () => {
|
||||
describe('request', () => {
|
||||
beforeEach(() => {
|
||||
const fetch = jest.fn();
|
||||
global.fetch = fetch;
|
||||
global.Date = jest.fn(() => ({ getTime: () => 1000 }));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
it('should fetch url with authorization header', async () => {
|
||||
const api = new API({
|
||||
api_root: 'https://site.netlify.com/.netlify/git/github',
|
||||
tokenPromise: () => Promise.resolve('token'),
|
||||
});
|
||||
|
||||
fetch.mockResolvedValue({
|
||||
text: jest.fn().mockResolvedValue('some response'),
|
||||
ok: true,
|
||||
status: 200,
|
||||
headers: { get: () => '' },
|
||||
});
|
||||
const result = await api.request('/some-path');
|
||||
expect(result).toEqual('some response');
|
||||
expect(fetch).toHaveBeenCalledTimes(1);
|
||||
expect(fetch).toHaveBeenCalledWith(
|
||||
'https://site.netlify.com/.netlify/git/github/some-path?ts=1000',
|
||||
{
|
||||
headers: { Authorization: 'Bearer token', 'Content-Type': 'application/json' },
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw error on not ok response with message property', async () => {
|
||||
const api = new API({
|
||||
api_root: 'https://site.netlify.com/.netlify/git/github',
|
||||
tokenPromise: () => Promise.resolve('token'),
|
||||
});
|
||||
|
||||
fetch.mockResolvedValue({
|
||||
text: jest.fn().mockResolvedValue({ message: 'some error' }),
|
||||
ok: false,
|
||||
status: 404,
|
||||
headers: { get: () => '' },
|
||||
});
|
||||
|
||||
await expect(api.request('some-path')).rejects.toThrow(
|
||||
expect.objectContaining({
|
||||
message: 'some error',
|
||||
name: 'API_ERROR',
|
||||
status: 404,
|
||||
api: 'Git Gateway',
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw error on not ok response with msg property', async () => {
|
||||
const api = new API({
|
||||
api_root: 'https://site.netlify.com/.netlify/git/github',
|
||||
tokenPromise: () => Promise.resolve('token'),
|
||||
});
|
||||
|
||||
fetch.mockResolvedValue({
|
||||
text: jest.fn().mockResolvedValue({ msg: 'some error' }),
|
||||
ok: false,
|
||||
status: 404,
|
||||
headers: { get: () => '' },
|
||||
});
|
||||
|
||||
await expect(api.request('some-path')).rejects.toThrow(
|
||||
expect.objectContaining({
|
||||
message: 'some error',
|
||||
name: 'API_ERROR',
|
||||
status: 404,
|
||||
api: 'Git Gateway',
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user