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)
}

On Sat, 15 Mar 2025 at 06:47, David Karr <davidmichaelk...@gmail.com> wrote:

> 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.
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion visit
> https://groups.google.com/d/msgid/golang-nuts/11b54912-08d2-41f4-9323-0bf6e37dd879n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/11b54912-08d2-41f4-9323-0bf6e37dd879n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/CA%2BhP-7vYkVFSYfVsteSe_PFop0nz8uHCry1XiLFga2Hw%3DKshBQ%40mail.gmail.com.

Reply via email to