2012-03-26 12 views
5

Play çağırır, ancak Scala tabanlı yöntemleri başlatırken benim girişimleri başarısız kavanozları. Scala Söz Add values to Session during testing (FakeRequest, FakeApplication) belirtilen bir çekme isteği dayanarak benim Java testlerinde 2.0'ın fakeRequest oyna kullanırken oturumu korumak için bir yol arıyorum 2.0'ın fakeRequest

, anladım Java işe yarayabilecek şu:

public Session getSession(Result result) { 
    play.api.mvc.Cookies scalaCookies = 
     play.api.test.Helpers.cookies(result.getWrappedResult()); 
    play.api.mvc.Cookie scalaSessionCookie = 
     scalaCookies.get(play.api.mvc.Session.COOKIE_NAME()).get(); 
    scala.Option<play.api.mvc.Cookie> optionalCookie = 
     scala.Option.apply(scalaSessionCookie); 

    // Compiles fine, but fails with NoSuchMethodError: 
    play.api.mvc.Session scalaSession = 
     play.api.mvc.Session.decodeFromCookie(optionalCookie); 

    return new play.mvc.Http.Session(Scala.asJava(scalaSession.data())); 
} 

Bu sadece iyi derler ama testlerini çalıştırırken o beni alır:

java.lang.NoSuchMethodError: 
    play.api.mvc.Session.decodeFromCookie(Lscala/Option;)Lplay/api/mvc/Session; 
hatta yakın olup olmadığımı

toplam Scala Newby olmak, gerçekten hiçbir fikrim yok. Scala Oturumu does expose (trait) that method through CookieBaker, I ,'u düşünür. İlle Yukarıdaki kod yayınlanmasını sağlamak için bir yol aramıyorum

Not; Yukarıdaki gerçekten seansı almak için ilk (mümkün) adımdır. Sonra muhtemelen sonraki isteklere iletmek için play.api.mvc.Session.encodeAsCookie(session) gibi bir şey kullanmayı deneyin. the ZenTasks demo için gibi: 1.x için

@Test 
public void testLoginAndMore() { 
    Helpers.running(Helpers.fakeApplication(Helpers.inMemoryDatabase()), 
    new Runnable() { 
    public void run() { 
     Map<String, String> data = new HashMap<String, String>(); 
     data.put("email", "[email protected]"); 
     data.put("password", "secret"); 

     Result result = 
     callAction(controllers.routes.ref.Application.authenticate(), 
      fakeRequest().withFormUrlEncodedBody(data)); 
     assertThat(status(result)).isEqualTo(Status.SEE_OTHER); 
     assertThat(redirectLocation(result)).isEqualTo("/"); 

     // All fine; we're logged in. Now somehow preserve the cookie. This 
     // does NOT do the trick: 
     Session session = getSession(result); 
     // ...subsequent callAction(..)s, somehow passing the session cookie 
    } 
    }); 
} 

, Playframework Secure module: how do you “log in” to test a secured controller in a FunctionalTest? yardımcı olur, ama işler 2.0 değişmiş gibi görünüyor ve ben 1.x hiç kullanılmamış

+0

(Ayrıca [Çal Çerçeve Google grubundaki ilgili yazı] (https://groups.google.com/forum/#!searchin/play-framework/session/play-framework/FuXaP7z9wz8) eklendi ..; eğer varsa denetlemeyi bekleyen, bu yazı ile senkronize olduğunu tutacak) Google'ın Gruplarda – Arjan

+0

Peter Hausel sadece yazmış: * Merhaba, çerez ve flaş desteği yanında (bu bugün için bir düzeltme itmek için gidiyorum). Teşekkürler peter * – Arjan

cevap

6

Pek sihirli sonuçta gerekli. Aşağıdaki basit çerezler ayarlar HTTP başlığı korur ve bir sonraki talebinde bu geçer.

Map<String, String> data = new HashMap<String, String>(); 
data.put("email", "[email protected]"); 
data.put("password", "secret"); 

Result result = callAction(controllers.routes.ref.Application.authenticate(), 
    fakeRequest().withFormUrlEncodedBody(data)); 

assertThat(status(result)).isEqualTo(Status.SEE_OTHER); 
assertThat(redirectLocation(result)).isEqualTo("/"); 
// All fine; we're logged in. Preserve the cookies: 
String cookies = header(HeaderNames.SET_COOKIE, result); 

// Fetch next page, passing the cookies 
result = routeAndCall(fakeRequest(GET, redirectLocation(result)) 
    .withHeader(HeaderNames.COOKIE, cookies)); 

assertThat(status(result)).isEqualTo(Status.OK); 
assertThat(contentAsString(result).contains("Guillaume Bort")); 

(sadece PLAY_SESSION çerez almak ve bu ayrıştırma hakkında bazı bilgiler için bu çok cevap the first version gör Bu pek bulunuyor gerçi gerekli.)

+0

Bu cevap için çok teşekkürler. – Mikesname

3

Play güncel sürümü testlerinde oturumu kullanmak ile gerçekten çok kolay. cookies(Result result) statik yardımcı yöntemini kullanabilirsiniz.

// Route that sets some session data to be used in subsequent requests 
Result result = callAction(...); 
Http.Cookie[] cookies = FluentIterable.from(cookies(result)).toArray(Http.Cookie.class); 

FakeRequest request = new FakeRequest(GET, "/someRoute").withCookies(cookies); 
callAction(controllers.routes.ref.Application.requestNeedingSession(), request); 
İlgili konular