Skip to content

Commit

Permalink
Automatically add slash to redirects if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
laurilehmijoki committed Jun 9, 2014
1 parent 067bae0 commit 55cd169
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This project uses [Semantic Versioning](http://semver.org).

## 2.1.6

* Automatically add slash to redirects if needed

## 2.1.5

* Target JVM 1.6 in build.sbt
Expand Down
2 changes: 1 addition & 1 deletion lib/s3_website/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module S3Website
VERSION = '2.1.5'
VERSION = '2.1.6'
end
11 changes: 10 additions & 1 deletion src/main/scala/s3/website/model/push.scala
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,18 @@ object Redirect {
config.redirects.fold(Nil: Seq[Redirect]) { sourcesToTargets =>
sourcesToTargets.foldLeft(Seq(): Seq[Redirect]) {
(redirects, sourceToTarget) =>
redirects :+ Redirect(sourceToTarget._1, sourceToTarget._2)
redirects :+ Redirect(sourceToTarget._1, applySlashIfNeeded(sourceToTarget._2))
}
}

private def applySlashIfNeeded(redirectTarget: String) = {
val isExternalRedirect = redirectTarget.matches("https?:\\/\\/.*")
val isInSiteRedirect = redirectTarget.startsWith("/")
if (isInSiteRedirect || isExternalRedirect)
redirectTarget
else
"/" + redirectTarget // let the user have redirect settings like "index.php: index.html" in s3_website.ml
}
}

case class S3File(s3Key: String, md5: MD5)
Expand Down
27 changes: 27 additions & 0 deletions src/test/scala/s3/website/S3WebsiteSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,33 @@ class S3WebsiteSpec extends Specification {
sentPutObjectRequest.getRedirectLocation must equalTo("/index.html")
}

"add slash to the redirect target" in new AllInSameDirectory with EmptySite with MockAWS with DefaultRunMode {
config = """
|redirects:
| index.php: index.html
""".stripMargin
push
sentPutObjectRequest.getRedirectLocation must equalTo("/index.html")
}

"support external redirects" in new AllInSameDirectory with EmptySite with MockAWS with DefaultRunMode {
config = """
|redirects:
| index.php: http://www.youtube.com/watch?v=dQw4w9WgXcQ
""".stripMargin
push
sentPutObjectRequest.getRedirectLocation must equalTo("http://www.youtube.com/watch?v=dQw4w9WgXcQ")
}

"support external redirects that point to an HTTPS target" in new AllInSameDirectory with EmptySite with MockAWS with DefaultRunMode {
config = """
|redirects:
| index.php: https://www.youtube.com/watch?v=dQw4w9WgXcQ
""".stripMargin
push
sentPutObjectRequest.getRedirectLocation must equalTo("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
}

"result in max-age=0 Cache-Control header on the object" in new AllInSameDirectory with EmptySite with MockAWS with DefaultRunMode {
config = """
|redirects:
Expand Down

0 comments on commit 55cd169

Please sign in to comment.