11import assert = require( 'assert' ) ;
2+ import fs = require( 'fs' ) ;
23import nock = require( 'nock' ) ;
4+ import os = require( 'os' ) ;
35import vsom = require( '../../api/VsoClient' ) ;
6+ import WebApi = require( '../../api/WebApi' ) ;
47import * as rm from 'typed-rest-client/RestClient' ;
5- import { resolve } from 'path' ;
68import { ApiResourceLocation } from '../../api/interfaces/common/VsoBaseInterfaces' ;
79
810describe ( 'VSOClient Units' , function ( ) {
@@ -27,7 +29,6 @@ describe('VSOClient Units', function () {
2729 it ( 'constructs' , ( ) => {
2830 //Arrange
2931 this . timeout ( 1000 ) ;
30- const baseUrl = 'https://dev.azure.com/' ;
3132 const userAgent : string = "testAgent" ;
3233 const rest : rm . RestClient = new rm . RestClient ( userAgent , null , [ ] ) ;
3334
@@ -182,4 +183,69 @@ describe('VSOClient Units', function () {
182183 //Assert
183184 assert ( res . id === "testLocation" ) ;
184185 } ) ;
186+ } ) ;
187+
188+ describe ( 'WebApi Units' , function ( ) {
189+ const osName : string = os . platform ( ) ;
190+ const osVersion : string = os . release ( ) ;
191+ const nodeApiName : string = 'azure-devops-node-api' ;
192+ const nodeApiVersion : string = JSON . parse ( fs . readFileSync ( 'package.json' , 'utf8' ) ) . version ;
193+
194+ it ( 'sets the user agent correctly when request settings are specified' , async ( ) => {
195+ const myWebApi : WebApi . WebApi = new WebApi . WebApi ( 'microsoft.com' , WebApi . getBasicHandler ( 'user' , 'password' ) ,
196+ undefined , { productName : 'name' , productVersion : '1.2.3' } ) ;
197+ const userAgent : string = `name/1.2.3 (${ nodeApiName } ${ nodeApiVersion } ; ${ osName } ${ osVersion } )` ;
198+ assert . equal ( userAgent , myWebApi . rest . client . userAgent , 'User agent should be: ' + userAgent ) ;
199+ } ) ;
200+
201+ it ( 'sets the user agent correctly when request settings are not specified' , async ( ) => {
202+ const myWebApi : WebApi . WebApi = new WebApi . WebApi ( 'microsoft.com' , WebApi . getBasicHandler ( 'user' , 'password' ) , undefined ) ;
203+ const userAgent : string = `${ nodeApiName } /${ nodeApiVersion } (${ osName } ${ osVersion } )` ;
204+ assert . equal ( userAgent , myWebApi . rest . client . userAgent , 'User agent should be: ' + userAgent ) ;
205+ } ) ;
206+
207+ it ( 'connects to the server with the correct user agent when request settings are specified' , async ( ) => {
208+ const myWebApi : WebApi . WebApi = new WebApi . WebApi ( 'https://dev.azure.com/' , WebApi . getBasicHandler ( 'user' , 'password' ) ,
209+ undefined , { productName : 'name' , productVersion : '1.2.3' } ) ;
210+ const userAgent : string = `name/1.2.3 (${ nodeApiName } ${ nodeApiVersion } ; ${ osName } ${ osVersion } )` ;
211+ nock ( 'https://dev.azure.com/_apis/testArea' , {
212+ reqheaders : {
213+ 'accept' : 'application/json' ,
214+ 'user-agent' : userAgent
215+ } } )
216+ . options ( '' )
217+ . reply ( 200 , {
218+ value : [ { id : 'testLocation' , maxVersion : '1' , releasedVersion : '1' , routeTemplate : 'testTemplate' , area : 'testArea' , resourceName : 'testName' , resourceVersion : '1' } ]
219+ } ) ;
220+
221+ // Act
222+ const res : vsom . ClientVersioningData = await myWebApi . vsoClient . getVersioningData ( '1' , 'testArea' , 'testLocation' , { 'testKey' : 'testValue' } , null ) ;
223+
224+ // Assert
225+ assert . equal ( res . apiVersion , '1' ) ;
226+ assert . equal ( res . requestUrl , 'https://dev.azure.com/testTemplate' ) ;
227+
228+ } ) ;
229+
230+ it ( 'connects to the server with the correct user agent when request settings are not specified' , async ( ) => {
231+ // Arrange
232+ const myWebApi : WebApi . WebApi = new WebApi . WebApi ( 'https://dev.azure.com/' , WebApi . getBasicHandler ( 'user' , 'password' ) , null ) ;
233+ const userAgent : string = `${ nodeApiName } /${ nodeApiVersion } (${ osName } ${ osVersion } )` ;
234+ nock ( 'https://dev.azure.com/_apis/testArea' , {
235+ reqheaders : {
236+ 'accept' : 'application/json' ,
237+ 'user-agent' : userAgent
238+ } } )
239+ . options ( '' )
240+ . reply ( 200 , {
241+ value : [ { id : 'testLocation' , maxVersion : '1' , releasedVersion : '1' , routeTemplate : 'testTemplate' , area : 'testArea' , resourceName : 'testName' , resourceVersion : '1' } ]
242+ } ) ;
243+
244+ // Act
245+ const res : vsom . ClientVersioningData = await myWebApi . vsoClient . getVersioningData ( '1' , 'testArea' , 'testLocation' , { 'testKey' : 'testValue' } , null ) ;
246+
247+ // Assert
248+ assert . equal ( res . apiVersion , '1' ) ;
249+ assert . equal ( res . requestUrl , 'https://dev.azure.com/testTemplate' ) ;
250+ } ) ;
185251} ) ;
0 commit comments