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.
On Friday, March 14, 2025 at 9:09:59 PM UTC-7 Lin Lin wrote: > 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 <davidmic...@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...@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/faa1c319-7ab6-44a9-9e8b-4aa801c929fcn%40googlegroups.com.