Comments (6)
it seems that each test request is indepenent
from akka-http-session.
maybe, I can add the cookie header in subsequent test request to make it work
from akka-http-session.
it seems that each test request is indepentent in ScalatestRouteTest, when I add Set-Cookie,
authorization is still failed。
package com.yimei.zflow.util.asset.routes
import akka.actor.ActorSystem
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpHeader, Multipart}
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.testkit.ScalatestRouteTest
import com.typesafe.config.ConfigFactory
import com.yimei.zflow.util.FlywayDB
import com.yimei.zflow.util.asset.routes.Models._
import com.yimei.zflow.util.organ.OrganSession
import org.scalatest.{Inside, Matchers, WordSpec}
object AssetRouteUT extends {
implicit val coreSystem: ActorSystem = ActorSystem("TestSystem", ConfigFactory.parseString(
"""
|database {
| jdbcUrl = "jdbc:mysql://127.0.0.1/cyflow?useUnicode=true&characterEncoding=utf8"
| username = "mysql"
| password = "mysql"
|}
|file.root = "/tmp"
|akka.http.session.server-secret = "1234567891234567891234567891234567891234567890000012345678901234"
|
""".stripMargin))
} with AssetRoute {
val fileField = "file"
def prepare() = {
val config = coreSystem.settings.config
val jdbcUrl = config.getString("database.jdbcUrl")
val username = config.getString("database.username")
val password = config.getString("database.password")
val flyway = new FlywayDB(jdbcUrl, username, password);
flyway.drop()
flyway.migrate()
}
import akka.http.scaladsl.server.Directives._
def loginRoute: Route = path("login") {
organSetSession(OrganSession("hary", "uid", "party", "instanceId", "company")) { ctx =>
ctx.complete("ok")
}
}
}
/**
* Created by hary on 17/1/10.
*/
class AssetRouteTest extends WordSpec with Matchers with Inside with ScalatestRouteTest with SprayJsonSupport {
import AssetRouteUT.{assetRoute, loginRoute, prepare}
override protected def beforeAll(): Unit = {
super.beforeAll()
prepare
// must be tested with login session
}
val xml = "<int>42</int>" // 上传文件
val simpleMultipartUpload =
Multipart.FormData(Multipart.FormData.BodyPart.Strict(
"file",
HttpEntity(ContentTypes.`text/xml(UTF-8)`, xml),
Map("filename" → "age.xml")))
var assetId: String = null
var cookie: HttpHeader = null
"AssetRouteTest" should {
"login is ok" in {
Post("/login") ~> loginRoute ~> check {
cookie = header("Set-Cookie").get
responseAs[String] shouldBe "ok"
println("cookie is " + cookie.toString)
}
}
"uploadFile is ok" in {
Post("/upload") ~> addHeader(cookie) ~> assetRoute ~> check {
inside(responseAs[UploadResult]) {
case UploadResult(id) =>
assetId = id
1 shouldBe 1
}
cookie = header("Set-Cookie").get
}
}
"downloadFile is in" in {
Get("/download/" + assetId) ~> addHeader(cookie) ~> assetRoute ~> check {
responseAs[String] shouldEqual xml
}
}
}
}
from akka-http-session.
Yes, each test is independent, only the headers that you specify explicitly are sent. Previous responses have no influence on subsequent requests.
Maybe this will help: https://github.com/softwaremill/akka-http-session/blob/master/core/src/test/scala/com/softwaremill/session/OneOffTest.scala#L94
These are the tests of the set/get session directives. In there the /set
endpoints sets a session (logs a user in), and /get
uses a previously created session.
from akka-http-session.
@adamw Can I pre-generate cookie/JWT before sending checking ANY route (like call JWT generate(resource)) ?
from akka-http-session.
@Hajto yes, you can do it through SessionManager.clientSessionManager.createCookie(data: T)
. You need to have an implicit session manager somewhere in order for the routes to work, so you can just use the same one.
from akka-http-session.
Related Issues (20)
- requireSession breaks CORS HOT 3
- RefreshTokenStorage schedule documentation HOT 5
- Infinite loop in RefreshTokenManager HOT 3
- Question: can Cassandra be used as "session"? HOT 2
- Question Regarding unresolved dependency: com.softwaremill#akka-http-session_2.12;0.5.2: not found HOT 2
- on secret management HOT 1
- Redirect unauthenticated request HOT 1
- Session token compatibility across upgrades HOT 8
- Please support Java 9! HOT 2
- Upgrade to akka-stream 2.5 HOT 2
- RSA signing for JWT HOT 1
- Build for Scala 2.13.0 HOT 1
- JWT: Add support for `iss` and `aud` claims HOT 1
- issue refreshing token HOT 7
- Allow separate access/refresh transport
- CSRF protection can be bypassed with empty header and empty cookie HOT 1
- Upgrade to akka streams 2.6.x HOT 2
- CSRF protection can be bypassed HOT 10
- Add SameSite attribute to Cookies HOT 1
- create pekko equivalent? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from akka-http-session.