Skip to content

Commit

Permalink
Set sourceFiles to empty array on workflowVersions endpoint (#780)
Browse files Browse the repository at this point in the history
* Set sourceFiles to empty array on workflowVersions endpoint

dockstore/dockstore#2833
dockstore/dockstore#2836
  • Loading branch information
coverbeck authored Sep 5, 2019
1 parent 15a61bf commit 327a8b3
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { HttpClient } from '@angular/common/http';
import { HTTP_INTERCEPTORS, HttpClient } from '@angular/common/http';
import { APP_INITIALIZER, NgModule } from '@angular/core';
import { FlexLayoutModule } from '@angular/flex-layout';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
Expand Down Expand Up @@ -98,6 +98,7 @@ import { StargazersModule } from './stargazers/stargazers.module';
import { StarredEntriesComponent } from './starredentries/starredentries.component';
import { StarringModule } from './starring/starring.module';
import { SessionExpiredComponent } from './session-expired/session-expired.component';
import { WorkflowVersionsInterceptor } from './interceptors/workflow-versions.interceptor';

export const myCustomTooltipDefaults: MatTooltipDefaultOptions = {
showDelay: 500,
Expand Down Expand Up @@ -212,7 +213,8 @@ export function configurationServiceFactory(configurationService: ConfigurationS
multi: true
},
{ provide: MAT_TOOLTIP_DEFAULT_OPTIONS, useValue: myCustomTooltipDefaults },
{ provide: MAT_SNACK_BAR_DEFAULT_OPTIONS, useValue: myCustomSnackbarDefaults }
{ provide: MAT_SNACK_BAR_DEFAULT_OPTIONS, useValue: myCustomSnackbarDefaults },
{ provide: HTTP_INTERCEPTORS, useClass: WorkflowVersionsInterceptor, multi: true }
],
entryComponents: [DeleteAccountDialogComponent, YoutubeComponent, ConfirmationDialogComponent],
bootstrap: [AppComponent]
Expand Down
49 changes: 49 additions & 0 deletions src/app/interceptors/workflow-versions.interceptor.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { WorkflowVersionsInterceptor } from './workflow-versions.interceptor';
import { inject, TestBed } from '@angular/core/testing';
import { HttpEvent, HttpHandler, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs';

class SourcefilesEmptyChecker extends HttpHandler {
handle(req: HttpRequest<any>): Observable<HttpEvent<any>> {
expect(req.body[0].sourceFiles).toEqual([]);
return undefined;
}
}

class RequestUnchangedChecker extends HttpHandler {
constructor(private origRequest: HttpRequest<any>) {
super();
}

handle(req: HttpRequest<any>): Observable<HttpEvent<any>> {
expect(req).toBe(this.origRequest);
return undefined;
}
}

describe('WorkflowVersionsInterceptor', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [WorkflowVersionsInterceptor]
});
});

it('Should tranform put', inject([WorkflowVersionsInterceptor], (interceptor: WorkflowVersionsInterceptor) => {
const httpRequest = new HttpRequest('PUT', 'https://dockstore.org/api/workflows/1234/workflowVersions', [
{ sourceFiles: [{ one: 'something' }] }
]);
const httpHandler = new SourcefilesEmptyChecker();
interceptor.intercept(httpRequest, httpHandler);
}));

it('Should leave other stuff alone', inject([WorkflowVersionsInterceptor], (interceptor: WorkflowVersionsInterceptor) => {
const body = [{ sourceFiles: [{ one: 'something' }] }];
const httpRequest1 = new HttpRequest('GET', 'https://dockstore.org/api/workflows/1234/workflowVersions', body);
const httpHandler = new RequestUnchangedChecker(httpRequest1);
interceptor.intercept(httpRequest1, httpHandler);

const httpRequest2 = new HttpRequest('PUT', 'https://dockstore.org/api/tools/1234/workflowVersions', body);
const httpHandler2 = new RequestUnchangedChecker(httpRequest2);
interceptor.intercept(httpRequest2, httpHandler2);
}));
});
44 changes: 44 additions & 0 deletions src/app/interceptors/workflow-versions.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright 2019 OICR
*
* 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 { Injectable } from '@angular/core';
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs';

const workflowVersionsEndpoint = /workflows\/\d+\/workflowVersions$/;

/**
* An interceptor that ensures that PUT requests on the workflowVersions
* endpoint have the sourceFiles property set to an empty array.
*
* The sourceFiles property is ignored by the web service endpoint, but can be
* sizeable. This affects performance and more importantly, can run into a max
* request limit size that we have on our nginx server.
*
* See https://github.com/dockstore/dockstore/issues/2833
*/
@Injectable()
export class WorkflowVersionsInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// In my tests, the body was an array of length 1. I assume it is an array to handle
// multipart form requests. To be safe, just handling arrays of size 1.
if (req.method === 'PUT' && workflowVersionsEndpoint.test(req.url) && req.body && req.body.length === 1) {
const emptySourceFiles = { sourceFiles: [] };
const newBody = [{ ...req.body[0], ...emptySourceFiles }];
return next.handle(req.clone({ body: newBody }));
}
return next.handle(req);
}
}

0 comments on commit 327a8b3

Please sign in to comment.