11var request = require ( 'supertest' ) ;
22var loopback = require ( 'loopback' ) ;
33var expect = require ( 'chai' ) . expect ;
4+ var query = require ( './util/query' ) ;
45var JSONAPIComponent = require ( '../' ) ;
56var app ;
67var Post ;
@@ -17,30 +18,56 @@ describe('loopback json api component create method', function () {
1718 } ) ;
1819 app . model ( Post ) ;
1920 app . use ( loopback . rest ( ) ) ;
20- JSONAPIComponent ( app ) ;
21+ JSONAPIComponent ( app , { restApiRoot : '' } ) ;
2122 } ) ;
2223
2324 describe ( 'headers' , function ( ) {
25+ it ( 'POST /models should be created when Accept header is set to application/vnd.api+json' , function ( done ) {
26+ request ( app ) . post ( '/posts' )
27+ . send ( {
28+ data : {
29+ type : 'posts' ,
30+ attributes : {
31+ title : 'my post' ,
32+ content : 'my post content'
33+ }
34+ }
35+ } )
36+ . set ( 'Accept' , 'application/vnd.api+json' )
37+ . set ( 'Content-Type' , 'application/json' )
38+ . expect ( 201 )
39+ . end ( done ) ;
40+ } ) ;
2441 it ( 'POST /models should have the JSON API Content-Type header set on response' , function ( done ) {
25- //TODO: superagent/supertest breaks when trying to use JSON API Content-Type header
42+ var data = {
43+ data : {
44+ type : 'posts' ,
45+ attributes : {
46+ title : 'my post' ,
47+ content : 'my post content'
48+ }
49+ }
50+ } ;
51+
52+ var options = {
53+ headers : {
54+ 'Accept' : 'application/vnd.api+json' ,
55+ 'Content-Type' : 'application/vnd.api+json'
56+ }
57+ } ;
58+
59+ //use http module via util to make this post to check headers are working since
60+ //superagent/supertest breaks when trying to use JSON API Content-Type header
2661 //waiting on a fix
2762 //see https://github.com/visionmedia/superagent/issues/753
28- //using Content-Type: application/json in the mean time.
29- //Have tested correct header using curl and all is well
30- // request(app).post('/posts')
31- // .send({
32- // data: {
33- // type: 'posts',
34- // attributes: {
35- // title: 'my post',
36- // content: 'my post content'
37- // }
38- // }
39- // })
40- // .set('Content-Type', 'application/vnd.api+json')
41- // .expect('Content-Type', 'application/vnd.api+json; charset=utf-8')
42- // .end(done);
43- done ( ) ;
63+ query ( app ) . post ( '/posts' , data , options , function ( err , res ) {
64+ if ( err ) console . log ( err ) ;
65+ expect ( res . headers [ 'content-type' ] ) . to . match ( / a p p l i c a t i o n \/ v n d \. a p i \+ j s o n / ) ;
66+ expect ( res . statusCode ) . to . equal ( 201 ) ;
67+ expect ( res . body ) . to . have . all . keys ( 'data' ) ;
68+ expect ( res . body . data ) . to . have . all . keys ( 'type' , 'id' , 'links' , 'attributes' ) ;
69+ done ( ) ;
70+ } ) ;
4471 } ) ;
4572
4673 it ( 'POST /models should have the Location header set on response' , function ( done ) {
0 commit comments