Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f8fab8d501 | ||
|
|
bb07fc7e6e | ||
|
|
7073bea751 | ||
|
|
b3fa7f0507 | ||
|
|
50a90591fc | ||
|
|
204cd377f5 | ||
|
|
988ee495ba |
16
README.md
16
README.md
@@ -1,4 +1,4 @@
|
||||
# yosoy
|
||||
# yosoy  
|
||||
|
||||
yosoy is a HTTP service for stubbing and prototyping distributed applications. It is a service which will introduce itself to the caller and print some useful information about its environment. "Yo soy" in español means "I am".
|
||||
|
||||
@@ -52,19 +52,19 @@ minikube start
|
||||
kubectl apply -f test/deployment.yaml
|
||||
# tunnel to it and copy the URL as $URL variable
|
||||
minikube service --url camarero
|
||||
# call it a few times
|
||||
curl $URL
|
||||
curl $URL
|
||||
curl $URL
|
||||
curl $URL
|
||||
# simulate some HTTP requests
|
||||
curl -H "Host: gateway.myapp.com" $URL/camarero/abc
|
||||
curl -H "Host: gateway.myapp.com" $URL/camarero/abc
|
||||
curl -H "Host: gateway.myapp.com" $URL/camarero/abc
|
||||
curl -H "Host: gateway.myapp.com" $URL/camarero/abc
|
||||
```
|
||||
|
||||
A sample response looks like this:
|
||||
|
||||
```json
|
||||
{
|
||||
"host": "127.0.0.1:53366",
|
||||
"requestUri": "/",
|
||||
"host": "gateway.myapp.com",
|
||||
"requestUri": "/camarero/abc",
|
||||
"remoteAddr": "172.17.0.1",
|
||||
"counter": 4,
|
||||
"headers": {
|
||||
|
||||
15
server.go
15
server.go
@@ -26,7 +26,19 @@ type response struct {
|
||||
var counter = 0
|
||||
var hostname = os.Getenv("HOSTNAME")
|
||||
|
||||
func preflight(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "*")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "*")
|
||||
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||
w.Header().Set("Access-Control-Expose-Headers", "*")
|
||||
w.Header().Set("Access-Control-Max-Age", "600")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func handler(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
|
||||
showEnvs := os.Getenv("YOSOY_SHOW_ENVS")
|
||||
showFiles := os.Getenv("YOSOY_SHOW_FILES")
|
||||
|
||||
@@ -80,7 +92,8 @@ func main() {
|
||||
r := mux.NewRouter()
|
||||
|
||||
r.Handle("/favicon.ico", r.NotFoundHandler)
|
||||
r.PathPrefix("/").HandlerFunc(handler)
|
||||
r.PathPrefix("/").HandlerFunc(preflight).Methods(http.MethodOptions)
|
||||
r.PathPrefix("/").HandlerFunc(handler).Methods(http.MethodGet, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete, http.MethodConnect, http.MethodHead, http.MethodTrace)
|
||||
|
||||
loggingRouter := handlers.CombinedLoggingHandler(os.Stdout, r)
|
||||
proxyRouter := handlers.ProxyHeaders(loggingRouter)
|
||||
|
||||
@@ -32,8 +32,12 @@ func TestHandler(t *testing.T) {
|
||||
var response response
|
||||
json.Unmarshal(rr.Body.Bytes(), &response)
|
||||
|
||||
// test response
|
||||
assert.Equal(t, 1, response.Counter)
|
||||
assert.Equal(t, "example.org", response.Host)
|
||||
assert.NotEmpty(t, response.EnvVariables)
|
||||
assert.NotEmpty(t, response.Files[".gitignore"])
|
||||
|
||||
// test cors
|
||||
assert.Contains(t, rr.HeaderMap["Access-Control-Allow-Origin"], "*")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user