2016-10-06 4 views
2

私はハピを使用していますが、これは私のハンドラ関数です:テスト時に自分のクッキーがハンドラ関数で利用できないのはなぜですか?

function propertyDetailsValidateHandler(request, reply, source, error) { 
console.log(request.state) 
var data = joiValidationHelper.checkForErrors(request, error); 
    if (typeof data !== "undefined"){ 
    return reply.view('property-details', data).code(400); 
    } else { 
    var details = request.state.details; 
    details.propertyType = request.payload.propertyType; 
    details.newBuild = request.payload.newBuild; 
    return reply.redirect('/property-details/postcode').state('details', details, {path: '/'}); 
    } 
} 

そして、これはジャスミン使って書かれた私のテストで:

describe('tell us about the property youre buying flow', function(){ 
    it('test /property-details, status code and location', function(done){ 
    var options = { 
     method: 'POST', 
     url: '/property-details', 
     headers: { 
     cookie: {details: { test: "test"}} 
     }, 
     payload: { 
     propertyType: "freehold", 
     newBuild: true 
     } 
    }; 
    server.inject(options, function(response){ 
     detailsTestCookie = response.headers['set-cookie'][0].split(';')[0]; 
     expect(response.statusCode).toBe(302); 
     expect(response.headers.location).toMatch("/property-details/postcode"); 
     done(); 
    }); 
    }); 
}) 

私は自分のサーバーを実行し、ブラウザを使用する場合、ハンドラ関数が正しく実行をテストを実行すると、request.stateは空のオブジェクトです。テストで提供したクッキーであることが予想されていたため、request.state.detailsが定義されていないため、テストが失敗します。これは私のテストでクッキーをヘッダに提供する正しい方法ですか?

+1

んクッキーは間違いなくブラウザに設定されていますか? response.statusCodeは間違いなく302テストですか? – Eoin

+0

Cookieがブラウザに確実に設定されます。 StatusCodeは、request.stateが空であるため、 'request.state.details'で壊れているので現在500を返しています – Ruth

答えて

2

このプロジェクトでは、テープとHapiを使用しています。

var cookie = the_cookie_you_want_to_send;

次に、あなたのテストのペイロードに:

headers: { cookie: `details=${cookie}`}

+0

これは無効なCookieメッセージ – Ruth

1

ものとしてエンコードするために必要なクッキーはクッキーを当社のサーバーファイルに登録された方法です。

server.state('details', { 
    ttl: null, 
    isSecure: false, 
    isHttpOnly: false, 
    encoding: 'base64json', //this is not encrypted just encoded 
    clearInvalid: false, // remove invalid cookies 
    strictHeader: false // don't allow violations of RFC 6265 
}); 
関連する問題