forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultSubmissionClient-spec.ts
More file actions
144 lines (114 loc) · 4.06 KB
/
DefaultSubmissionClient-spec.ts
File metadata and controls
144 lines (114 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { expect } from 'chai';
import { Configuration } from '../configuration/Configuration';
import { IEvent } from '../models/IEvent';
import { IUserDescription } from '../models/IUserDescription';
import { DefaultSubmissionClient } from './DefaultSubmissionClient';
import { ISubmissionAdapter } from './ISubmissionAdapter';
import { ISubmissionClient } from './ISubmissionClient';
import { SubmissionCallback } from './SubmissionCallback';
import { SubmissionRequest } from './SubmissionRequest';
class TestAdapter implements ISubmissionAdapter {
private request;
private checks: Array<(request: SubmissionRequest) => void > = [];
private callback: SubmissionCallback;
private status = 202;
private message = null;
private data;
private headers;
constructor(check: (request: SubmissionRequest) => void) {
this.checks.push(check);
}
public withResponse(status: number, message: string, data?: string, headers?: any) {
this.status = status;
this.message = message;
this.data = data;
this.headers = headers;
return this;
}
public withCheck(check: (request: SubmissionRequest) => void) {
this.checks.push(check);
return this;
}
public sendRequest(request: SubmissionRequest, callback?: SubmissionCallback, isAppExiting?: boolean) {
this.request = request;
this.callback = callback;
if (isAppExiting) {
this.done();
}
}
public done() {
if (!this.request) {
expect.fail('sendRequest hasn\'t been called.');
return;
}
this.checks.forEach((c) => c(this.request));
this.callback && this.callback(this.status, this.message, this.data, this.headers);
}
}
describe('DefaultSubmissionClient', () => {
let adapter: TestAdapter;
let config: Configuration;
let submissionClient: ISubmissionClient;
beforeEach(() => {
const apiKey = 'LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw';
const serverUrl = 'http://localhost:50000';
submissionClient = new DefaultSubmissionClient();
config = new Configuration({
apiKey,
serverUrl
});
adapter = new TestAdapter((r) => {
expect(r.apiKey).to.equal(apiKey);
});
config.submissionAdapter = adapter;
});
it('should submit events', (done) => {
const events = [{ type: 'log', message: 'From js client', reference_id: '123454321' }];
adapter.withCheck((r) => {
expect(r.data).to.equal(JSON.stringify(events));
expect(r.method).to.equal('POST');
expect(r.url).to.equal(`${config.serverUrl}/api/v2/events`);
});
submissionClient.postEvents(events, config, () => done());
adapter.done();
});
it('should submit invalid object data', (done) => {
const events: IEvent[] = [{
type: 'log', message: 'From js client', reference_id: '123454321', data: {
name: 'blake',
age: () => { throw new Error('Test'); }
}
}];
adapter.withCheck((r) => {
expect(r.data).to.equal(JSON.stringify(events));
expect(r.method).to.equal('POST');
expect(r.url).to.equal(`${config.serverUrl}/api/v2/events`);
});
submissionClient.postEvents(events, config, () => done());
adapter.done();
});
it('should submit user description', (done) => {
const description: IUserDescription = {
email_address: 'norply@exceptionless.io',
description: 'unit test'
};
adapter.withCheck((r) => {
expect(r.data).to.equal(JSON.stringify(description));
expect(r.method).to.equal('POST');
expect(r.url).to.equal(`${config.serverUrl}/api/v2/events/by-ref/123454321/user-description`);
});
submissionClient.postUserDescription('123454321', description, config, () => done());
adapter.done();
});
it('should get project settings', (done) => {
adapter.withResponse(200, null, JSON.stringify({ version: 1 }));
submissionClient.getSettings(config, 0, (response) => {
expect(response.success).to.be.true;
expect(response.message).to.be.null;
expect(response.settings).not.to.be.null;
expect(response.settingsVersion).to.be.greaterThan(-1);
done();
});
adapter.done();
});
});