add a responseparser that only looks at the status code, and use it for statistic event responses

This commit is contained in:
Art Lowel
2019-12-16 18:43:09 +01:00
parent a78e0512cb
commit de6abb7ce6
3 changed files with 115 additions and 0 deletions

View File

@@ -0,0 +1,82 @@
import { StatusCodeOnlyResponseParsingService } from './status-code-only-response-parsing.service';
describe('StatusCodeOnlyResponseParsingService', () => {
let service;
let statusCode;
let statusText;
beforeEach(() => {
service = new StatusCodeOnlyResponseParsingService();
});
describe('parse', () => {
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);
});
});
});
});

View File

@@ -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);
}
}

View File

@@ -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;
}
}