1
+ /* Chakram is an API testing framework designed to perform end to end tests on JSON REST endpoints. */
2
+ /* Chakram offers a BDD testing style through Chakram's expect interface. */
3
+
4
+ var chakram = require ( 'chakram' ) ;
5
+ var expect = chakram . expect ;
6
+
7
+ // simple response
8
+ describe ( "Chakram" , function ( ) {
9
+ it ( "should offer simple HTTP request capabilities" , function ( ) {
10
+ var response = chakram . get ( "http://httpbin.org/get" ) ;
11
+ return response ;
12
+ } ) ;
13
+ } ) ;
14
+
15
+ // response 200
16
+ describe ( "Chakram" , function ( ) {
17
+ it ( "should provide HTTP specific assertions" , function ( ) {
18
+ var response = chakram . get ( "http://httpbin.org/get" ) ;
19
+ var response = expect ( response ) . to . have . status ( 200 ) ;
20
+ return response ;
21
+ } ) ;
22
+ } ) ;
23
+
24
+ describe ( "HTTP assertions" , function ( ) {
25
+ it ( "should make HTTP assertions easy" , function ( ) {
26
+ var response = chakram . get ( "http://httpbin.org/get?test=chakram" ) ;
27
+ expect ( response ) . to . have . status ( 200 ) ;
28
+ expect ( response ) . to . have . header ( "content-type" , "application/json" ) ;
29
+ expect ( response ) . not . to . be . encoded . with . gzip ;
30
+ expect ( response ) . to . comprise . of . json ( {
31
+ args : { test : "chakram" }
32
+ } ) ;
33
+ var waitedResponse = chakram . wait ( ) ;
34
+ return waitedResponse ;
35
+ } ) ;
36
+ } ) ;
37
+
38
+ // wait for response
39
+ describe ( "Chakram" , function ( ) {
40
+ it ( "should provide a simple async testing framework" , function ( ) {
41
+ var response = chakram . get ( "http://httpbin.org/get" ) ;
42
+ expect ( response ) . to . have . status ( 200 ) ;
43
+ expect ( response ) . not . to . have . header ( 'non-existing-header' ) ;
44
+ var waitedResponse = chakram . wait ( ) ;
45
+ return waitedResponse ;
46
+ } ) ;
47
+ } ) ;
48
+
49
+ // Complex Promise Use
50
+ // https://github.com/request/request#requestoptions-callback
51
+ // Simplified HTTP request client.
52
+ describe ( "Chakram" , function ( ) {
53
+ it ( "should support sequential API interaction" , function ( ) {
54
+ var requestParams = {
55
+ headers : {
56
+ 'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'
57
+ }
58
+ } ;
59
+ var response = chakram . get ( "https://api.github.com/" , requestParams )
60
+ . then ( function ( apiResponse ) {
61
+ var emojisUrl = apiResponse . body . emojis_url ;
62
+ var response = chakram . get ( emojisUrl , requestParams ) ;
63
+ return response ;
64
+ } )
65
+ . then ( function ( emojiList ) {
66
+ var cinema = emojiList . body . cinema ;
67
+ expect ( cinema ) . to . contain ( "images/icons/emoji" ) ;
68
+ } ) ;
69
+ return response ;
70
+ } ) ;
71
+ } ) ;
72
+
73
+ describe ( "BDD + Hooks" , function ( ) {
74
+ var thingName ;
75
+ before ( "post dweet" , function ( ) {
76
+ thingName = "chakramtest" + Math . floor ( Math . random ( ) * 2000 ) ;
77
+ var postResponse = chakram . post ( "https://dweet.io/dweet/for/" + thingName , {
78
+ testing : "your API"
79
+ } ) ;
80
+ return postResponse ;
81
+ } ) ;
82
+
83
+ it ( "should support getting latest dweet" , function ( ) {
84
+ var postedData = chakram . get ( "https://dweet.io/get/latest/dweet/for/" + thingName ) ;
85
+ return expect ( postedData ) . to . have . json ( 'with[0].content' , {
86
+ testing : "your API"
87
+ } ) ;
88
+ } ) ;
89
+
90
+ after ( "update dweet with result" , function ( ) {
91
+ return chakram . post ( "https://dweet.io/dweet/for/" + thingName , {
92
+ testing : "passed"
93
+ } ) ;
94
+ } ) ;
95
+ } ) ;
96
+
97
+ // add new property
98
+ describe ( "Extensibility" , function ( ) {
99
+ before ( "define teapot" , function ( ) {
100
+ chakram . addProperty ( "teapot" , function ( respObj ) {
101
+ var statusCode = respObj . response . statusCode ;
102
+ this . assert ( statusCode === 418 ,
103
+ 'expected status code ' + statusCode + ' to equal 418' ,
104
+ 'expected ' + statusCode + ' to not be equal to 418' ) ;
105
+ } ) ;
106
+ } ) ;
107
+
108
+ it ( "should be able to detect teapots" , function ( ) {
109
+ var notATeapot = chakram . get ( "http://httpbin.org/status/200" ) ;
110
+ var aTeapot = chakram . get ( "http://httpbin.org/status/418" ) ;
111
+ expect ( notATeapot ) . to . not . be . teapot ;
112
+ expect ( aTeapot ) . to . be . teapot ;
113
+ var waitedResponse = chakram . wait ( ) ;
114
+ return waitedResponse ;
115
+ } ) ;
116
+ } ) ;
0 commit comments