-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
126 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* jslint node: true, esnext: true */ | ||
|
||
'use strict'; | ||
|
||
const pcs = require('parse-concat-stream'), | ||
Interceptor = require('kronos-interceptor').Interceptor; | ||
|
||
/** | ||
* | ||
*/ | ||
class DecodeJSONInterceptor extends Interceptor { | ||
static get name() { | ||
return 'decode-json'; | ||
} | ||
|
||
get type() { | ||
return DecodeJSONInterceptor.name; | ||
} | ||
|
||
receive(request, args) { | ||
return new Promise((fullfilled, rejected) => | ||
request.payload.pipe(pcs((err, data) => { | ||
if (err) { | ||
rejected(err); | ||
} else { | ||
fullfilled({ | ||
data: data | ||
}); | ||
} | ||
}))); | ||
} | ||
} | ||
|
||
exports.decode = DecodeJSONInterceptor; | ||
|
||
exports.registerWithManager = manager => Promise.all([ | ||
manager.registerInterceptor(DecodeJSONInterceptor), | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* global describe, it, xit */ | ||
/* jslint node: true, esnext: true */ | ||
|
||
'use strict'; | ||
|
||
const chai = require('chai'), | ||
assert = chai.assert, | ||
expect = chai.expect, | ||
should = chai.should(), | ||
fs = require('fs'), | ||
path = require('path'), | ||
kti = require('kronos-test-interceptor'), | ||
JSONDecodeInterceptor = require('../decode-json').decode; | ||
|
||
const mochaInterceptorTest = kti.mochaInterceptorTest, | ||
testResponseHandler = kti.testResponseHandler; | ||
|
||
const logger = { | ||
debug(a) { | ||
console.log(a); | ||
} | ||
}; | ||
|
||
/* simple owner with name */ | ||
function dummyEndpoint(name) { | ||
return { | ||
get name() { | ||
return name; | ||
}, | ||
get path() { | ||
return '/get:id'; | ||
}, | ||
toString() { | ||
return this.name; | ||
}, | ||
"step": logger | ||
}; | ||
} | ||
|
||
describe('interceptors', () => { | ||
const ep = dummyEndpoint('ep'); | ||
|
||
mochaInterceptorTest(JSONDecodeInterceptor, ep, {}, 'decode-json', (itc, withConfig) => { | ||
if (!withConfig) return; | ||
|
||
describe('json', () => { | ||
it('toJSON', () => { | ||
assert.deepEqual(itc.toJSON(), { | ||
type: 'decode-json' | ||
}); | ||
}); | ||
}); | ||
|
||
itc.connected = dummyEndpoint('ep'); | ||
itc.connected.receive = testResponseHandler; | ||
|
||
it('passing request', () => itc.receive({ | ||
payload: fs.createReadStream(path.join(__dirname, 'fixtures/simple.json')) | ||
}).then((response) => { | ||
assert.deepEqual(response, { | ||
data: { | ||
a: 1 | ||
} | ||
}); | ||
})); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"a": 1 | ||
} |