Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { StatusCodeOnlyResponseParsingService } from './status-code-only-response-parsing.service';

describe('StatusCodeOnlyResponseParsingService', () => {
let service;
let statusCode;
let statusText;

beforeEach(() => {
service = new StatusCodeOnlyResponseParsingService();
});

describe('parse', () => {

it('should return a RestResponse that doesn\'t contain the response body', () => {
const payload = 'd9128e44-183b-479d-aa2e-d39435838bf6';
const result = service.parse(undefined, {
payload,
statusCode: 201,
statusText: '201'
});

expect(JSON.stringify(result).indexOf(payload)).toBe(-1);
});

describe('when the response is successful', () => {
beforeEach(() => {
statusCode = 201;
statusText = `${statusCode}`;
});

it('should return a success RestResponse', () => {
const result = service.parse(undefined, {
statusCode,
statusText
});

expect(result.isSuccessful).toBe(true);
});

it('should return a RestResponse with the correct status code', () => {
const result = service.parse(undefined, {
statusCode,
statusText
});

expect(result.statusCode).toBe(statusCode);
});

it('should return a RestResponse with the correct status text', () => {
const result = service.parse(undefined, {
statusCode,
statusText
});

expect(result.statusText).toBe(statusText);
});
});

describe('when the response is unsuccessful', () => {
beforeEach(() => {
statusCode = 400;
statusText = `${statusCode}`;
});

it('should return an error RestResponse', () => {
const result = service.parse(undefined, {
statusCode,
statusText
});

expect(result.isSuccessful).toBe(false);
});

it('should return a RestResponse with the correct status code', () => {
const result = service.parse(undefined, {
statusCode,
statusText
});

expect(result.statusCode).toBe(statusCode);
});

it('should return a RestResponse with the correct status text', () => {
const result = service.parse(undefined, {
statusCode,
statusText
});

expect(result.statusText).toBe(statusText);
});
});
});
});
26 changes: 26 additions & 0 deletions src/app/core/data/status-code-only-response-parsing.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Injectable } from '@angular/core';
import { RestResponse } from '../cache/response.models';
import { DSpaceRESTV2Response } from '../dspace-rest-v2/dspace-rest-v2-response.model';
import { ResponseParsingService } from './parsing.service';
import { RestRequest } from './request.models';

/**
* A responseparser that will only look at the status code and status
* text of the response, and ignore anything else that might be there
*/
@Injectable({
providedIn: 'root'
})
export class StatusCodeOnlyResponseParsingService implements ResponseParsingService {

/**
* Parse the response and only extract the status code and status text
*
* @param request The request that was sent to the server
* @param data The response to parse
*/
parse(request: RestRequest, data: DSpaceRESTV2Response): RestResponse {
const isSuccessful = data.statusCode >= 200 && data.statusCode < 300;
return new RestResponse(isSuccessful, data.statusCode, data.statusText);
}
}
7 changes: 7 additions & 0 deletions src/app/statistics/track-request.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { ResponseParsingService } from '../core/data/parsing.service';
import { PostRequest } from '../core/data/request.models';
import { StatusCodeOnlyResponseParsingService } from '../core/data/status-code-only-response-parsing.service';
import { GenericConstructor } from '../core/shared/generic-constructor';

export class TrackRequest extends PostRequest {

getResponseParser(): GenericConstructor<ResponseParsingService> {
return StatusCodeOnlyResponseParsingService;
}
}