iamport 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,6 +11,14 @@ var Promise = require('bluebird');
11
11
  var _ = require('lodash');
12
12
  var hasOwn = {}.hasOwnProperty;
13
13
 
14
+ function pathSegment(value) {
15
+ var segment = String(value);
16
+ if (value == null || segment === '' || segment === '.' || segment === '..') {
17
+ throw new Error('Invalid path segment');
18
+ }
19
+ return encodeURIComponent(segment);
20
+ }
21
+
14
22
  /** method 생성 함수
15
23
  * API path 생성
16
24
  * URL Param, Form Param require check
@@ -20,7 +28,7 @@ var hasOwn = {}.hasOwnProperty;
20
28
  */
21
29
  function iamportMethod (spec) {
22
30
  var requestMethod = (spec.method || 'GET').toUpperCase();
23
- return function(param) {
31
+ return Promise.method(function(param) {
24
32
  const stack = new Error().stack
25
33
  param = param || {};
26
34
 
@@ -34,7 +42,7 @@ function iamportMethod (spec) {
34
42
 
35
43
  if(spec.urlParam) {
36
44
  if( hasOwn.call(param, spec.urlParam) ) {
37
- apiParams.push( param[spec.urlParam] );
45
+ apiParams.push(pathSegment(param[spec.urlParam]));
38
46
  param = _.omit(param, spec.urlParam);
39
47
  } else {
40
48
  return new Promise(function(resolve, reject) {
@@ -49,7 +57,9 @@ function iamportMethod (spec) {
49
57
 
50
58
  if(spec.urlParam2) {
51
59
  if( hasOwn.call(param, spec.urlParam2) ) {
52
- apiParams.push( param[spec.urlParam2] );
60
+ if (param[spec.urlParam2] != null && param[spec.urlParam2] !== '') {
61
+ apiParams.push(pathSegment(param[spec.urlParam2]));
62
+ }
53
63
  param = _.omit(param, spec.urlParam2);
54
64
  }
55
65
  // urlParam2 is optional - no error if missing
@@ -84,7 +94,7 @@ function iamportMethod (spec) {
84
94
  reject(error);
85
95
  })
86
96
  });
87
- };
97
+ });
88
98
  }
89
99
 
90
100
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "iamport",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "REST API client for I'amport;",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ var assert = require('assert');
4
+ var Iamport = require('..');
5
+
6
+ describe('request construction', function() {
7
+ it('encodes both path parameters and keeps query fields separate', function() {
8
+ var client = Iamport();
9
+ client.payment._makeRequest = function(method, url) {
10
+ assert.strictEqual(method, 'GET');
11
+ assert.strictEqual(url, client._host + '/payments/findAll/order%2F1/status%3F?page=2');
12
+ return Promise.resolve();
13
+ };
14
+ return client.payment.getAllByMerchant({ merchant_uid: 'order/1', payment_status: 'status?', page: 2 });
15
+ });
16
+
17
+ it('rejects invalid required and optional segments before dispatch', function() {
18
+ var client = Iamport();
19
+ var calls = [
20
+ client.payment.getByImpUid({ imp_uid: '' }),
21
+ client.payment.getByImpUid({ imp_uid: '.' }),
22
+ client.payment.getAllByMerchant({ merchant_uid: 'order.1', payment_status: '..' })
23
+ ];
24
+ return Promise.all(calls.map(function(call) {
25
+ return call.then(function() { assert.fail('Expected rejection'); }, function(error) {
26
+ assert.strictEqual(error.message, 'Invalid path segment');
27
+ });
28
+ }));
29
+ });
30
+
31
+ it('omits an empty optional status from the path and query', function() {
32
+ var client = Iamport();
33
+ client.payment._makeRequest = function(method, url) {
34
+ assert.strictEqual(url, client._host + '/payments/findAll/order?page=2');
35
+ return Promise.resolve();
36
+ };
37
+ return client.payment.getAllByMerchant({ merchant_uid: 'order', payment_status: '', page: 2 });
38
+ });
39
+
40
+ });