yasuoza diary

web and life

Set another IP on scalatra spec

When filtering client IP on scalatra, the following code will work

1
2
3
4
5
6
get("/") {
  if (!ConfigData.allowedIP.contains(request.remoteAddress)) {
    halt(403, "Forbbiden")
  }
  scaml("hello-scalate")
}

Spec file will be like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
"GET / on MyServlet from 127.0.0.1" should {
  "return status 200" in {
    get("/", Seq.empty, Map("X-FORWARDED-FOR" -> "127.0.0.1")) {
      status must_== 200
    }
  }
}
"GET / on MyServlet from 0.0.0.0" should {
  "return status 403" in {
    get("/", Seq.empty, Map("X-FORWARDED-FOR" -> "0.0.0.0")) {
      status must_== 403
    }
  }
}

Comments