Skip to content

Commit d7093f6

Browse files
committed
Added isContentBody to Communicator
1 parent 2484d01 commit d7093f6

File tree

2 files changed

+98
-6
lines changed

2 files changed

+98
-6
lines changed

src/communicator.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Communicator {
99
static METHOD_GET = 'GET';
1010
static METHOD_DELETE = 'DELETE';
1111
static METHOD_POST = 'POST';
12+
static CONTENT_BODY_KEY = 'Content-Body';
1213

1314
baseUrl = 'https://ws3.morpher.ru';
1415
token = null;
@@ -34,13 +35,17 @@ class Communicator {
3435
}
3536
}
3637

37-
request(path, params, method) {
38+
request(path, params = new Map(), method = Communicator.METHOD_GET) {
3839

39-
const url = this.buildUrl(path, params);
40+
const isContentBody = this.isContentBody(params, method);
4041

41-
const contentType = this.isPost(method)
42-
? 'application/x-www-form-urlencoded'
43-
: 'application/json';
42+
const url = isContentBody
43+
? this.buildUrl(path)
44+
: this.buildUrl(path, params);
45+
46+
const contentType = isContentBody
47+
? 'text/plain; charset=utf-8'
48+
: 'application/x-www-form-urlencoded';
4449

4550
const init = {
4651
method: method,
@@ -50,6 +55,10 @@ class Communicator {
5055
},
5156
};
5257

58+
if (isContentBody) {
59+
init.body = params.get(Communicator.CONTENT_BODY_KEY);
60+
}
61+
5362
return new Promise((resolve, reject) => {
5463
const timer = setTimeout(
5564
() => reject(new Error('TIMEOUT')),
@@ -66,7 +75,7 @@ class Communicator {
6675
});
6776
}
6877

69-
buildUrl(path, params) {
78+
buildUrl(path, params = new Map()) {
7079
let url = new URL(path, this.baseUrl);
7180

7281
params.forEach((value, name) => {
@@ -92,6 +101,11 @@ class Communicator {
92101
return this._fetcher;
93102
}
94103

104+
isContentBody(params, method) {
105+
return this.isPost(method) && params.size === 1 &&
106+
params.has(Communicator.CONTENT_BODY_KEY);
107+
}
108+
95109
isPost(method) {
96110
return method.toUpperCase() === Communicator.METHOD_POST;
97111
}

test/unit/communicator.test.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,86 @@ describe('Communicator', function() {
108108
communicator.buildUrl(path, params),
109109
baseUrl + path + '?key_1=value&key_2=value_1%2Cvalue_2',
110110
);
111+
112+
communicator = new Communicator();
113+
assert.equal(
114+
communicator.buildUrl(path),
115+
communicator.baseUrl + path,
116+
);
117+
});
118+
119+
});
120+
121+
describe('#isContentBody()', function() {
122+
123+
const communicator = new Communicator();
124+
125+
it('should return false when no parameters are specified', function() {
126+
return assert.equal(
127+
communicator.isContentBody(new Map(), Communicator.METHOD_POST),
128+
false,
129+
);
111130
});
112131

132+
it('should return false when the \'Content-Body\' parameter is not specified',
133+
function() {
134+
const params = new Map();
135+
params.set('k', 0);
136+
137+
return assert.equal(
138+
communicator.isContentBody(params, Communicator.METHOD_POST),
139+
false,
140+
);
141+
},
142+
);
143+
144+
it('should return false when more than one parameter is specified',
145+
function() {
146+
const params = new Map();
147+
params.set('k', 0);
148+
params.set(Communicator.CONTENT_BODY_KEY, 'текст');
149+
150+
return assert.equal(
151+
communicator.isContentBody(params, Communicator.METHOD_POST),
152+
false,
153+
);
154+
},
155+
);
156+
157+
it('should return false when the method is not \'post\'',
158+
function() {
159+
const params = new Map();
160+
params.set(Communicator.CONTENT_BODY_KEY, 'текст');
161+
162+
assert.equal(
163+
communicator.isContentBody(params, Communicator.METHOD_GET),
164+
false,
165+
);
166+
167+
assert.equal(
168+
communicator.isContentBody(params, 'put'),
169+
false,
170+
);
171+
172+
assert.equal(
173+
communicator.isContentBody(params, 'delete'),
174+
false,
175+
);
176+
},
177+
);
178+
179+
it('should return true when the \'post\' method, the parameter is the only one, and it\'s named \'Content-Body\'',
180+
function() {
181+
const params = new Map();
182+
params.set(Communicator.CONTENT_BODY_KEY, 'текст');
183+
184+
assert.equal(
185+
communicator.isContentBody(params, Communicator.METHOD_POST),
186+
true,
187+
);
188+
},
189+
);
190+
113191
});
114192

115193
describe('#isPost()', function() {

0 commit comments

Comments
 (0)