Skip to content
Open
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
Expand Up @@ -17,27 +17,56 @@
* under the License.
*/

import { ComponentFixture, TestBed } from '@angular/core/testing';

import { MonitorFormComponent } from './monitor-form.component';

describe('MonitorFormComponent', () => {
let component: MonitorFormComponent;
let fixture: ComponentFixture<MonitorFormComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [MonitorFormComponent]
}).compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(MonitorFormComponent);
component = fixture.componentInstance;
fixture.detectChanges();
component = new MonitorFormComponent(
{
info: () => undefined,
error: () => undefined
} as any,
{
fanyi: (key: string) => key
} as any
);
component.monitor = { name: 'api monitor', scheduleType: 'interval' } as any;
component.collector = '';
component.sdParams = [];
component.paramDefines = [];
component.sdDefines = [];
component.advancedParamDefines = [{ depend: { httpMethod: ['POST'] } } as any];
});

it('should create', () => {
expect(component).toBeTruthy();
});

it('should clear payload when httpMethod changes to a method without body', () => {
component.advancedParams = [{ field: 'payload', paramValue: 'old body' } as any];

component.onDependChanged('GET', 'httpMethod');

expect(component.advancedParams[0].paramValue).toBe('');
});

it('should keep payload when httpMethod changes to POST-like methods', () => {
component.advancedParams = [{ field: 'payload', paramValue: 'old body' } as any];

component.onDependChanged('POST', 'httpMethod');

expect(component.advancedParams[0].paramValue).toBe('old body');
});

it('should clear payload before submitting a GET monitor', () => {
component.params = [{ field: 'httpMethod', paramValue: 'GET' } as any];
component.advancedParams = [{ field: 'payload', paramValue: 'old body' } as any];
spyOn(component.formSubmit, 'emit');

component.onSubmit({ invalid: false } as any);

expect(component.advancedParams[0].paramValue).toBe('');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export class MonitorFormComponent implements OnChanges {
param.paramValue = (param.paramValue as string).trim();
}
});
this.clearPayloadIfHttpMethodHasNoBody(this.params.find(param => param.field === 'httpMethod')?.paramValue);

// Set monitor.instance to host param value, let backend handle the port concatenation
const hostParam = this.params.find(param => param.field === 'host');
Expand Down Expand Up @@ -170,6 +171,7 @@ export class MonitorFormComponent implements OnChanges {
param.paramValue = (param.paramValue as string).trim();
}
});
this.clearPayloadIfHttpMethodHasNoBody(this.params.find(param => param.field === 'httpMethod')?.paramValue);

// Set monitor.instance to host param value, let backend handle the port concatenation
const hostParam = this.params.find(param => param.field === 'host');
Expand Down Expand Up @@ -265,6 +267,19 @@ export class MonitorFormComponent implements OnChanges {
}
}
});
if (dependField === 'httpMethod') {
this.clearPayloadIfHttpMethodHasNoBody(dependValue);
}
}

private clearPayloadIfHttpMethodHasNoBody(httpMethod: unknown): void {
if (httpMethod == null || ['POST', 'PUT', 'PATCH'].includes(String(httpMethod).toUpperCase())) {
return;
}
const payloadParam = this.advancedParams?.find(param => param.field === 'payload');
if (payloadParam) {
payloadParam.paramValue = '';
}
}

//start grafana
Expand Down
Loading