Skip to content

Commit

Permalink
Add /request endpoint to perform test requests
Browse files Browse the repository at this point in the history
  • Loading branch information
orlangure committed May 15, 2022
1 parent 2a79804 commit 5c0a6ae
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ value to `GET /env1` on port 80.

When started with environment variable `GNOMOCK_TEST_2`, it replies with its
value to `GET /env2` on port 8080.

When started with environment variable `GNOMOCK_REQUEST_TARGET`, it performs a
`GET` request to that URL (i.e `http://container-name`) and returns the same
status code it received from the target, every time it receives a new request
to `/request` endpoint.
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,23 @@ import (
func main() {
env1 := os.Getenv("GNOMOCK_TEST_1")
env2 := os.Getenv("GNOMOCK_TEST_2")
requestTarget := os.Getenv("GNOMOCK_REQUEST_TARGET")

fmt.Println("received args:", os.Args[1:])
fmt.Printf("starting with env1 = '%s', env2 = '%s'\n", env1, env2)

mux80 := http.NewServeMux()
mux80.HandleFunc("/", echoHandler("80"))
mux80.HandleFunc("/env1", echoHandler(env1))
mux80.HandleFunc("/request", func(w http.ResponseWriter, r *http.Request) {
res, err := http.Get(requestTarget)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.WriteHeader(res.StatusCode)
})

mux8080 := http.NewServeMux()
mux8080.HandleFunc("/", echoHandler("8080"))
Expand Down

0 comments on commit 5c0a6ae

Please sign in to comment.