-
Notifications
You must be signed in to change notification settings - Fork 22
Implement mechanism to serve files via custom protocols #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import sbt.Keys._ | |
|
||
import org.scalajs.jsenv.selenium.SeleniumJSEnv | ||
import org.scalajs.jsenv.selenium.Firefox | ||
import org.scalajs.jsenv.selenium.CustomFileMaterializer | ||
|
||
val commonSettings: Seq[Setting[_]] = Seq( | ||
version := "0.1.2-SNAPSHOT", | ||
|
@@ -18,6 +19,15 @@ val commonSettings: Seq[Setting[_]] = Seq( | |
Some("scm:git:[email protected]:scala-js/scala-js-env-selenium.git"))) | ||
) | ||
|
||
val testSettings: Seq[Setting[_]] = commonSettings ++ Seq( | ||
testOptions += | ||
Tests.Argument(TestFramework("com.novocode.junit.JUnitFramework"), "-v", "-a"), | ||
jsDependencies ++= Seq( | ||
RuntimeDOM % "test", | ||
"org.webjars" % "jquery" % "1.10.2" / "jquery.js" | ||
) | ||
) | ||
|
||
// We'll need the name scalajs-env-selenium for the `seleniumJSEnv` project | ||
name := "root" | ||
|
||
|
@@ -65,13 +75,16 @@ lazy val seleniumJSEnv: Project = project. | |
lazy val seleniumJSEnvTest: Project = project. | ||
enablePlugins(ScalaJSPlugin). | ||
enablePlugins(ScalaJSJUnitPlugin). | ||
settings(commonSettings). | ||
settings(testSettings). | ||
settings( | ||
testOptions += | ||
Tests.Argument(TestFramework("com.novocode.junit.JUnitFramework"), "-v", "-a"), | ||
jsDependencies ++= Seq( | ||
RuntimeDOM % "test", | ||
"org.webjars" % "jquery" % "1.10.2" / "jquery.js" | ||
), | ||
jsEnv := new SeleniumJSEnv(Firefox) | ||
) | ||
|
||
lazy val seleniumJSHttpEnvTest: Project = project. | ||
enablePlugins(ScalaJSPlugin). | ||
enablePlugins(ScalaJSJUnitPlugin). | ||
settings(testSettings). | ||
settings( | ||
jsEnv := new SeleniumJSEnv(Firefox). | ||
withMaterializer(new CustomFileMaterializer("tmp", "http://localhost:8080/tmp")) | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
seleniumJSEnv/src/main/scala/org/scalajs/jsenv/selenium/CustomFileMaterializer.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.scalajs.jsenv.selenium | ||
|
||
import java.io.File | ||
import java.net.URL | ||
|
||
import org.scalajs.core.tools.io.{IO, VirtualTextFile, WritableFileVirtualTextFile} | ||
|
||
/** Materializes files on the filesystem and specifies a custom url to access stored files. | ||
* This can be used to bypass cross origin access policies as shown below. | ||
* | ||
* @param fsRoot Location on the filesystem where to store the generated files | ||
* @param webRoot Corresponding url to access the files | ||
* | ||
* @example | ||
* | ||
* The following illustrates how to configure a project such that the browser fetches | ||
* files by http:// instead of file://. | ||
* This example assumes a local webserver is running and serving the ".tmp" | ||
* directory at http://localhost:8080 | ||
* | ||
* <pre> | ||
* jsSettings( | ||
* // ... | ||
* jsEnv := new SeleniumJSEnv(org.scalajs.jsenv.selenium.Firefox) | ||
* .withMaterializer(new SpecificFileMaterializer(".tmp", "http://localhost:8080")) | ||
* ) | ||
* </pre> | ||
*/ | ||
class CustomFileMaterializer(val fsRoot: String, val webRoot: String) extends FileMaterializer { | ||
|
||
val storageDir = createStorageDir() | ||
|
||
/** Create a target file to write/copy to. Will also call | ||
* deleteOnExit on the file. | ||
*/ | ||
private def trgFile(name: String): File = { | ||
val f = new File(storageDir, name) | ||
f.deleteOnExit() | ||
f | ||
} | ||
|
||
/** Creates the storage directory if it does not exist. */ | ||
private def createStorageDir(): File = { | ||
val storageDir = new File(fsRoot) | ||
storageDir.mkdir() | ||
storageDir | ||
} | ||
|
||
override def materialize(vf: VirtualTextFile): URL = { | ||
val trg = trgFile(vf.name) | ||
IO.copyTo(vf, WritableFileVirtualTextFile(trg)) | ||
new URL(webRoot + "/" + vf.name) | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
seleniumJSEnv/src/main/scala/org/scalajs/jsenv/selenium/DefaultFileMaterializer.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.scalajs.jsenv.selenium | ||
|
||
import org.scalajs.core.tools.io.VirtualTextFile | ||
import org.scalajs.jsenv.VirtualFileMaterializer | ||
import java.net.URL | ||
|
||
/** Materializes virtual files in a temporary directory and links to them | ||
* via file:// | ||
*/ | ||
object DefaultFileMaterializer extends FileMaterializer { | ||
|
||
private val materializer = new VirtualFileMaterializer(true) | ||
|
||
override def materialize(vf: VirtualTextFile): URL = { | ||
materializer.materialize(vf).toURI.toURL | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
seleniumJSEnv/src/main/scala/org/scalajs/jsenv/selenium/FileMaterializer.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.scalajs.jsenv.selenium | ||
|
||
import java.net.URL | ||
|
||
import org.scalajs.core.tools.io.VirtualTextFile | ||
|
||
trait FileMaterializer { | ||
def materialize(vf: VirtualTextFile): URL | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
seleniumJSHttpEnvTest/src/test/scala/org/scalajs/jsenv/selenium/LocationTest.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.scalajs.jsenv.selenium | ||
|
||
import org.junit.Assert._ | ||
import org.junit.Test | ||
|
||
import scala.scalajs.js.Dynamic.global | ||
|
||
class LocationTest { | ||
|
||
@Test def LocationTest(): Unit = { | ||
assertEquals("http:", global.window.location.protocol.toString()) | ||
assertEquals("localhost:8080", global.window.location.host.toString()) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment should be formatted as follows.