Golang httptest server circular dependency

Issue

I’d like to write a test for a function that

  1. Makes a Get request to url1, which retrieves url2
  2. Makes a Get request to url2, and returns the result

But I’m not sure how to mock the return value for url2, since I cannot fetch the server.URL before the server is started. But after the server is started I cannot change the handler. Eg, running the below gives error Get /url2: unsupported protocol scheme ""

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "net/http/httptest"
)

func myFunc(client *http.Client, url1 string) string {
    url2 := get(client, url1)
    return get(client, url2)
}

func get(client *http.Client, url string) string {
    resp, err := client.Get(url)
    if err != nil {
        fmt.Println(err)
    }
    body, err := ioutil.ReadAll(resp.Body)
    defer resp.Body.Close()
    if err != nil {
        fmt.Println(err)
    }
    return string(body)
}

// test myFunc
func main() {
    srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        switch r.URL.String() {
        case "/url1":
            w.Write([]byte("/url2")) // how to specify srv.URL+"/url2" here?
        case "/url2":
            w.Write([]byte("return data"))
        }
    }))
    defer srv.Close()

    myFunc(srv.Client(), srv.URL+"/url1")
}

Source: https://onlinegdb.com/UpKlXfw45

Solution

Declare the srv variable before using the variable.

var srv *httptest.Server
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    switch r.URL.String() {
    case "/url1":
        w.Write([]byte(srv.URL + "/url2")) 
    case "/url2":
        w.Write([]byte("return data"))
    }
}))

Answered By – morris

Answer Checked By – Marie Seifert (GoLangFix Admin)

Leave a Reply

Your email address will not be published.