Can't figure out why simple mockserver doesn't hit the handler

114 views
Skip to first unread message

David Karr

unread,
Mar 14, 2025, 6:47:25 PMMar 14
to golang-nuts
A little while ago, I was able to write some unit tests for some code that makes an http connection.  I created a mockserver and a client using that server. That test in that application works fine.

I'm now trying to do basically the same thing in a different application. I'm trying to start with just a low-level handler that gets called for anything, and I'll iterate it into something useful. However, for some reason my CUT (code under test) is simply not calling my mocked handler, and I don't understand why. I must be making a simple mistake, but I'm just not seeing it.

My test looks like this so far (not actually testing anything yet):

func TestProcessProjectSimple(t *testing.T) {
    reader, writer, err := os.Pipe()
    if err != nil {
        t.Fatal(err)
    }
    defer reader.Close()
    defer writer.Close()

    handler := func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(http.StatusOK)
    }

    server := newMockServer(handler)
    defer server.Close()

    scmType = Github
    githubHttpClient = NewHTTPClient(server.Client(), "abc")

    processProject(writer, "projectName")
}

func newMockServer(handlerFunc http.HandlerFunc) *httptest.Server {
    return httptest.NewServer(handlerFunc)
}

The "processProject" method makes a call like this:

githubHttpClient.fetchGithubRepositoriesInOrg(outputFile, projectName)

That method has a receiver definition of "(hc *HTTPClientData)" and makes a call like this:

            resp, err := hc.client.Do(req)
When I step over that line, I'm expecting it to step into my one-line handler defined earlier, but it does not.

Jason E. Aten

unread,
Mar 14, 2025, 11:55:19 PMMar 14
to golang-nuts
Typically http servers start a goroutine per client. If your mock server is doing that
(without seeing the code, we cannot tell), a debugger won't step between the client 
and server goroutines. Put print statements in to tell what is actually happening.
I have a little library I use constantly to debug in Go. I end up embedding in
most of my applications. It turns out to be radically
faster to print rather than use a debugger; and printing doesn't care how many goroutines
you have going at once. https://github.com/glycerine/vprint

Lin Lin

unread,
Mar 15, 2025, 12:09:59 AMMar 15
to David Karr, golang-nuts
Hi, David, I'm no expert on http std. But the following testcase based on the httptest unittest code works. I  think the key point is to use ' client.Get(server.URL)' to invoke a request.
Hope it helps.

import "testing"
import "os"
import "net/http"
import "net/http/httptest"


func TestProcessProjectSimple(t *testing.T) {
        reader, writer, err := os.Pipe()
        if err != nil {
            t.Fatal(err)
        }
        defer reader.Close()
        defer writer.Close()

        handler := func(w http.ResponseWriter, r *http.Request) {
                t.Log("hi")

                w.WriteHeader(http.StatusOK)
        }

        server := newMockServer(handler)
        defer server.Close()

        client := server.Client()
        client.Get(server.URL)

}

func newMockServer(handlerFunc http.HandlerFunc) *httptest.Server {
        return httptest.NewServer(handlerFunc)
}
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/11b54912-08d2-41f4-9323-0bf6e37dd879n%40googlegroups.com.

David Karr

unread,
Mar 15, 2025, 1:12:50 PMMar 15
to golang-nuts
Ok, I figured out the basic problem. I had neglected to configure the server url in the httpclient object used by both the CUT and the test, so the request wasn't being sent to the mock server or the mock handler. Thanks for the responses. Some of that will be useful anyway. I didn't realize the testing object had a Log method.
Reply all
Reply to author
Forward
0 new messages