Skip to content
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

Add /request endpoint to perform test requests #1

Merged
merged 1 commit into from
May 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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