browsh/interfacer/test/http-server/setup.go
Thomas Buckley-Houston 0fc39a51e6 HTTP Server service: fetches URL, returns raw text
Using the `-http-server` argument will now start Browsh in HTTP Server
mode. It will accept request like this:

  `curl brow.sh/http://news.ycombinator.com`

This will return a plain text version of the Hacker News front page,
with a width of 100 characters, with each line separated by a line
break.
2018-05-27 20:45:43 +08:00

63 lines
1.4 KiB
Go

package test
import (
"fmt"
"time"
"net/http"
"io/ioutil"
ginkgo "github.com/onsi/ginkgo"
"browsh/interfacer/src/browsh"
)
var staticFileServerPort = "4444"
var rootDir = browsh.Shell("git rev-parse --show-toplevel")
func startStaticFileServer() {
serverMux := http.NewServeMux()
serverMux.Handle("/", http.FileServer(http.Dir(rootDir + "/interfacer/test/sites")))
http.ListenAndServe(":" + staticFileServerPort, serverMux)
}
func startBrowsh() {
browsh.IsTesting = true
*browsh.IsHTTPServer = true
browsh.HTTPServerStart()
}
func getPath(path string) string {
browshServiceBase := "http://localhost:" + *browsh.HTTPServerPort
staticFileServerBase := "http://localhost:" + staticFileServerPort
fullBase := browshServiceBase + "/" + staticFileServerBase
response, err := http.Get(fullBase + path)
if err != nil {
panic(fmt.Sprintf("%s", err))
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("%s", err)
panic(fmt.Sprintf("%s", err))
}
return string(contents)
}
}
var _ = ginkgo.BeforeEach(func() {
browsh.IsMonochromeMode = false
browsh.Log("\n---------")
browsh.Log(ginkgo.CurrentGinkgoTestDescription().FullTestText)
browsh.Log("---------")
})
var _ = ginkgo.BeforeSuite(func() {
go startStaticFileServer()
go startBrowsh()
time.Sleep(10 * time.Second)
})
var _ = ginkgo.AfterSuite(func() {
browsh.Shell(rootDir + "/webext/contrib/firefoxheadless.sh kill")
})