@@ -2,79 +2,147 @@ import { Configuration } from '../configuration/Configuration';
22import { IEvent } from '../models/IEvent';
33import { IUserDescription } from '../models/IUserDescription';
44import { ISubmissionClient } from './ISubmissionClient';
5+ import { ISubmissionAdapter } from './ISubmissionAdapter';
56import { DefaultSubmissionClient } from './DefaultSubmissionClient';
67import { SettingsResponse } from './SettingsResponse';
8+ import { SubmissionCallback } from './SubmissionCallback';
9+ import { SubmissionRequest } from './SubmissionRequest';
710import { SubmissionResponse } from './SubmissionResponse';
811
9- describe('DefaultSubmissionClient', () => {
10- it('should submit events', (done) => {
11- function processResponse(response:SubmissionResponse) {
12- if (response.success) {
13- expect(response.message).toBe(null);
14- } else {
15- expect(response.message).toBe('Unable to connect to server.');
16- }
1712
18- done();
19- }
13+ class TestAdapter implements ISubmissionAdapter {
14+ private request;
15+ private checks: { (request: SubmissionRequest):void }[] = [];
16+ private callback:SubmissionCallback;
17+ private status = 202;
18+ private message = null;
19+ private data;
20+ private headers;
2021
21- var config = new Configuration({ apiKey:'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', serverUrl:'http://localhost:50000'});
22- var submissionClient = new DefaultSubmissionClient();
23- submissionClient.postEvents([{ type: 'log', message: 'From js client', reference_id: '123454321' }], config, processResponse);
24- }, 5000);
22+ constructor(check: (request: SubmissionRequest) => void) {
23+ this.checks.push(check);
24+ }
2525
26- it('should submit invalid object data', (done) => {
27- function processResponse(response:SubmissionResponse) {
28- if (response.success) {
29- expect(response.message).toBe(null) ;
30- } else {
31- expect(response.message).toBe('Unable to connect to server.') ;
32- }
26+ public withResponse(status:number, message:string, data?:string, headers?:any) {
27+ this.status = status;
28+ this.message = message;
29+ this.data = data ;
30+ this.headers = headers;
31+ return this ;
32+ }
3333
34- done();
34+ public withCheck(check: (request: SubmissionRequest) => void) {
35+ this.checks.push(check);
36+ return this;
37+ }
38+
39+ public sendRequest(request:SubmissionRequest, callback:SubmissionCallback, isAppExiting?:boolean) {
40+ this.request = request;
41+ this.callback = callback;
42+
43+ if (isAppExiting) {
44+ this.done();
3545 }
46+ }
47+
48+ public done(){
49+ if (!this.request) {
50+ fail("sendRequest hasn't been called.");
51+ return;
52+ }
53+
54+ this.checks.forEach(c => c(this.request));
55+ this.callback(this.status, this.message, this.data, this.headers);
56+ }
57+ }
58+
59+ describe('DefaultSubmissionClient', () => {
60+
61+ var adapter:TestAdapter;
62+ var config:Configuration;
63+ var submissionClient: ISubmissionClient;
64+
65+ beforeEach(()=>{
66+
67+ var apiKey = 'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw';
68+ var serverUrl = 'http://localhost:50000'
69+
70+ submissionClient = new DefaultSubmissionClient();
3671
37- var config = new Configuration({ apiKey:'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', serverUrl:'http://localhost:50000'});
38- var event:IEvent = { type: 'log', message: 'From js client', reference_id: '123454321', data: {
72+ config = new Configuration({
73+ apiKey,
74+ serverUrl
75+ });
76+
77+ adapter = new TestAdapter(r => {
78+ expect(r.apiKey).toBe(apiKey);
79+ expect(r.serverUrl).toBe(serverUrl);
80+ });
81+
82+ config.submissionAdapter = adapter;
83+ });
84+
85+ it('should submit events', (done) => {
86+ var events = [{ type: 'log', message: 'From js client', reference_id: '123454321' }];
87+
88+ adapter.withCheck(r => {
89+ expect(r.data).toBe(JSON.stringify(events));
90+ expect(r.method).toBe('POST');
91+ expect(r.path).toBe('/api/v2/events');
92+ });
93+
94+ submissionClient.postEvents(events, config, () => done());
95+
96+ adapter.done();
97+ });
98+
99+ it('should submit invalid object data', (done) => {
100+ var events:IEvent[] = [{ type: 'log', message: 'From js client', reference_id: '123454321', data: {
39101 name: 'blake',
40102 age: function() { throw new Error('Test'); }
41- }};
103+ }}] ;
42104
43- var submissionClient = new DefaultSubmissionClient();
44- submissionClient.postEvents([event], config, processResponse);
45- }, 5000);
105+ adapter.withCheck(r => {
106+ expect(r.data).toBe(JSON.stringify(events));
107+ expect(r.method).toBe('POST');
108+ expect(r.path).toBe('/api/v2/events');
109+ });
110+
111+ submissionClient.postEvents(events, config, () => done());
112+
113+ adapter.done();
114+ });
46115
47116 it('should submit user description', (done) => {
48- function processResponse(response:SubmissionResponse) {
49- if (response.success) {
50- expect(response.message).toBe(null);
51- } else {
52- expect(response.message).toBe('Unable to connect to server.');
53- }
117+ var description = {
118+ email_address: 'norply@exceptionless.io',
119+ description: 'unit test'
120+ };
54121
55- done();
56- }
122+ adapter.withCheck(r => {
123+ expect(r.data).toBe(JSON.stringify(description));
124+ expect(r.method).toBe('POST');
125+ expect(r.path).toBe('/api/v2/events/by-ref/123454321/user-description')
126+ });
57127
58- var config = new Configuration({ apiKey:'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', serverUrl:'http://localhost:50000'} );
59- var submissionClient = new DefaultSubmissionClient();
60- submissionClient.postUserDescription('123454321', { email_address: 'norply@exceptionless.io', description: 'unit test' }, config, processResponse)
61- }, 5000 );
128+ submissionClient.postUserDescription('123454321', description, config, () => done() );
129+
130+ adapter.done();
131+ });
62132
63133 it('should get project settings', (done) => {
64- function processResponse(response:SettingsResponse) {
65- if (response.success) {
66- expect(response.message).toBe(null);
67- expect(response.settings).not.toBe(null);
68- expect(response.settingsVersion).toBeGreaterThan(-1 );
69- } else {
70- expect(response.message). toBe('Unable to connect to server.' );
71- }
134+
135+ adapter.withResponse(200, null, JSON.stringify({ version: 1 }));
136+
137+ submissionClient.getSettings(config, response => {
138+ expect(response.success).toBe(true );
139+ expect(response.message).toBe(null);
140+ expect(response.settings).not. toBe(null );
141+ expect(response.settingsVersion).toBeGreaterThan(-1);
72142
73143 done();
74- }
144+ });
75145
76- var config = new Configuration({ apiKey:'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw', serverUrl:'http://localhost:50000'});
77- var submissionClient = new DefaultSubmissionClient();
78- submissionClient.getSettings(config, processResponse);
79- }, 5000);
146+ adapter.done();
147+ });
80148});
0 commit comments