Correctly handle slugs in POST requests

* bug: correctly handle slug in POST request

* bug: disallow slashes in slug + modified tests

* fix: fixed tests to work with PUT instead of POST+slug

* fix: fixed tests failing in ci

* fix: adapted to reviews

* fix: adapted to review
This commit is contained in:
Arthur Joppart
2021-02-24 12:03:41 +01:00
committed by GitHub
parent 894d4589d9
commit 28c0eb7e88
6 changed files with 154 additions and 103 deletions

View File

@@ -2,7 +2,6 @@ import { EventEmitter } from 'events';
import { promises as fs } from 'fs';
import type { IncomingHttpHeaders } from 'http';
import { Readable } from 'stream';
import * as url from 'url';
import type { MockResponse } from 'node-mocks-http';
import { createResponse } from 'node-mocks-http';
import type { ResourceStore, PermissionSet, HttpHandler, HttpRequest } from '../../src/';
@@ -93,24 +92,22 @@ export class ResourceHelper {
return response;
}
public async createResource(fileLocation: string, slug: string, contentType: string, mayFail = false):
public async createResource(fileLocation: string, path: string, contentType: string, mayFail = false):
Promise<MockResponse<any>> {
const fileData = await fs.readFile(
joinFilePath(__dirname, fileLocation),
);
const response: MockResponse<any> = await this.performRequestWithBody(
this.baseUrl,
'POST',
new URL(path, this.baseUrl),
'PUT',
{ 'content-type': contentType,
slug,
'transfer-encoding': 'chunked' },
fileData,
);
if (!mayFail) {
expect(response.statusCode).toBe(201);
expect(response.statusCode).toBe(205);
expect(response._getData()).toHaveLength(0);
expect(response._getHeaders().location).toContain(url.format(this.baseUrl));
}
return response;
}
@@ -153,20 +150,18 @@ export class ResourceHelper {
return response;
}
public async createContainer(slug: string): Promise<MockResponse<any>> {
public async createContainer(path: string): Promise<MockResponse<any>> {
const response: MockResponse<any> = await this.performRequest(
this.baseUrl,
'POST',
new URL(path, this.baseUrl),
'PUT',
{
slug,
link: '<http://www.w3.org/ns/ldp#Container>; rel="type"',
'content-type': 'text/turtle',
'transfer-encoding': 'chunked',
},
);
expect(response.statusCode).toBe(201);
expect(response.statusCode).toBe(205);
expect(response._getData()).toHaveLength(0);
expect(response._getHeaders().location).toContain(url.format(this.baseUrl));
return response;
}