File tree Expand file tree Collapse file tree 2 files changed +29
-4
lines changed Expand file tree Collapse file tree 2 files changed +29
-4
lines changed Original file line number Diff line number Diff line change @@ -79,6 +79,23 @@ describe('tone_analyzer.v3', function() {
7979 assert . equal ( req . headers [ 'accept' ] , 'application/json' ) ;
8080 } ) ;
8181
82+ it ( 'tone API should add optional language parameter' , function ( ) {
83+ const options = {
84+ text : tone_request . text ,
85+ tones : 'emotion' ,
86+ sentences : true ,
87+ language : 'en'
88+ } ;
89+ const req = tone_analyzer . tone ( options , noop ) ;
90+ const body = Buffer . from ( req . body ) . toString ( 'ascii' ) ;
91+ assert . equal ( req . uri . href , service . url + tone_path + '?version=2016-05-19&tones=emotion&sentences=true' ) ;
92+ assert . equal ( body , tone_request . text ) ;
93+ assert . equal ( req . method , 'POST' ) ;
94+ assert . equal ( req . headers [ 'content-type' ] , 'text/plain' ) ;
95+ assert . equal ( req . headers [ 'accept' ] , 'application/json' ) ;
96+ assert . equal ( req . headers [ 'content-language' ] , 'en' ) ;
97+ } ) ;
98+
8299 it ( 'tone API should set HTML content-type' , function ( ) {
83100 const options = { text : tone_request . text , isHTML : true } ;
84101 const req = tone_analyzer . tone ( options , noop ) ;
Original file line number Diff line number Diff line change @@ -57,6 +57,7 @@ ToneAnalyzerV3.URL = 'https://gateway.watsonplatform.net/tone-analyzer/api';
5757 * - `isHTML`: A boolean value telling that the `params.text` argument is
5858 * to be trated as HTML contents. On HTML input, the services does
5959 * cleanup of tags and performs the analysis on the text contents only.
60+ * - `language`: Language of the input. It defaults to `en`.
6061 *
6162 * @return upon success, the callback function is called with an object
6263 * containing the different tones (emotion, writing and social) analyzed.
@@ -70,6 +71,16 @@ ToneAnalyzerV3.prototype.tone = function(params, callback) {
7071 return ;
7172 }
7273 const contentType = params . isHTML ? 'text/html' : 'text/plain' ;
74+
75+ const headers = {
76+ accept : 'application/json' ,
77+ 'content-type' : contentType
78+ } ;
79+
80+ if ( params . language ) {
81+ headers [ 'content-language' ] = params . language ;
82+ }
83+
7384 const parameters = {
7485 options : {
7586 url : '/v3/tone' ,
@@ -78,10 +89,7 @@ ToneAnalyzerV3.prototype.tone = function(params, callback) {
7889 qs : pick ( params , [ 'tones' , 'sentences' ] )
7990 } ,
8091 defaultOptions : extend ( true , this . _options , {
81- headers : {
82- accept : 'application/json' ,
83- 'content-type' : contentType
84- }
92+ headers : headers
8593 } )
8694 } ;
8795
You can’t perform that action at this time.
0 commit comments