Created
November 17, 2014 21:07
-
-
Save johannespetzold/434559f278ce4a46f55a to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"log" | |
"net/http" | |
"os" | |
"github.com/cloudfoundry-incubator/receptor/cmd/receptor/testrunner" | |
"github.com/cloudfoundry/storeadapter/storerunner/etcdstorerunner" | |
"github.com/onsi/ginkgo" | |
"github.com/onsi/gomega" | |
"github.com/onsi/gomega/gexec" | |
"github.com/tedsuo/ifrit" | |
"github.com/tedsuo/ifrit/grouper" | |
"github.com/tedsuo/ifrit/http_server" | |
"github.com/tedsuo/ifrit/sigmon" | |
) | |
const ( | |
EtcdPort = 4001 | |
EtcdUrl = "http://127.0.0.1:4001" | |
ReceptorAddress = "bar.com:6700" | |
TestAddress = "foo.com:8081" | |
Username = "user" | |
Password = "pass" | |
) | |
const Html = ` | |
<html> | |
<head></head> | |
<body> | |
<div id="output"></div> | |
<script type="text/javascript"> | |
window.onload = function(){ | |
console.log("starting CORS request") | |
var req = new XMLHttpRequest(); | |
req.withCredentials = true; | |
req.onreadystatechange = function(){ | |
console.log("readyState",this.readyState) | |
if (this.readyState == 4) { | |
console.log("Finished CORS request",this.responseText); | |
} | |
} | |
req.open("delete", "http://` + ReceptorAddress + `/v1/tasks/abcd", true, "` + Username + `", "` + Password + `"); | |
req.send() | |
} | |
</script> | |
</body> | |
</html> | |
` | |
var handler http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte(Html)) | |
} | |
var etcdRunner ifrit.RunFunc = func(signals <-chan os.Signal, ready chan<- struct{}) error { | |
etcdProcess := etcdstorerunner.NewETCDClusterRunner(EtcdPort, 1) | |
etcdProcess.Start() | |
close(ready) | |
<-signals | |
etcdProcess.Stop() | |
return nil | |
} | |
func main() { | |
gomega.RegisterFailHandler(ginkgo.Fail) | |
receptorBinPath, err := gexec.Build("github.com/cloudfoundry-incubator/receptor/cmd/receptor") | |
exitOnError(err) | |
defer gexec.CleanupBuildArtifacts() | |
receptorArgs := testrunner.Args{ | |
Address: ReceptorAddress, | |
EtcdCluster: EtcdUrl, | |
Username: Username, | |
Password: Password, | |
CORSEnabled: true, | |
} | |
group := grouper.NewOrdered(os.Interrupt, grouper.Members{ | |
{"etcd", etcdRunner}, | |
{"receptor", testrunner.New(receptorBinPath, receptorArgs)}, | |
{"cors-test", http_server.New(TestAddress, handler)}, | |
}) | |
log.Print("CORS Test starting") | |
process := ifrit.Invoke(sigmon.New(group)) | |
log.Print("CORS Test started") | |
err = <-process.Wait() | |
exitOnError(err) | |
} | |
func exitOnError(err error) { | |
if err != nil { | |
log.Print("FATAL ERROR", err) | |
os.Exit(1) | |
} | |
} |
these are the most amazing acceptance instructions in the universe
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
/etc/hosts
:go run main.go
http://foo.com:8081/
in your browser while watching javascript consoleFinished CORS request {"name":"TaskNotFound","message":"task guid not found"}
CORSEnabled: false
in main.go, you should seeXMLHttpRequest cannot load http://bar.com:6700/v1/tasks/abcd. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://foo.com:8081' is therefore not allowed access. The response had HTTP status code 401.