MCPcopy Index your code
hub / github.com/google/go-github / newTestServer

Function newTestServer

tools/metadata/main_test.go:335–384  ·  view source on GitHub ↗
(t *testing.T, ref string, files map[string]any)

Source from the content-addressed store, hash-verified

333}
334
335func newTestServer(t *testing.T, ref string, files map[string]any) *httptest.Server {
336 t.Helper()
337 jsonHandler := func(wantQuery url.Values, val any) http.HandlerFunc {
338 return func(w http.ResponseWriter, r *http.Request) {
339 gotQuery := r.URL.Query()
340 queryDiff := cmp.Diff(wantQuery, gotQuery)
341 if queryDiff != "" {
342 t.Errorf("query mismatch for %v (-want +got):\n%v", r.URL.Path, queryDiff)
343 }
344 w.WriteHeader(200)
345 err := json.NewEncoder(w).Encode(val)
346 if err != nil {
347 panic(err)
348 }
349 }
350 }
351 repoPath := "/api/v3/repos/github/rest-api-description"
352 emptyQuery := url.Values{}
353 refQuery := url.Values{"ref": []string{ref}}
354 mux := http.NewServeMux()
355 server := httptest.NewServer(mux)
356 mux.HandleFunc(
357 path.Join(repoPath, "commits", ref),
358 jsonHandler(emptyQuery, &github.RepositoryCommit{SHA: github.Ptr("s")}),
359 )
360 var descriptionsContent []*github.RepositoryContent
361 for name, content := range files {
362 descriptionsContent = append(descriptionsContent, &github.RepositoryContent{
363 Name: github.Ptr(path.Base(path.Dir(name))),
364 })
365 mux.HandleFunc(
366 path.Join(repoPath, "contents/descriptions", name),
367 jsonHandler(refQuery, &github.RepositoryContent{
368 Name: github.Ptr(path.Base(name)),
369 DownloadURL: github.Ptr(server.URL + "/dl/" + name),
370 }),
371 )
372 mux.HandleFunc(
373 path.Join("/dl", name),
374 jsonHandler(emptyQuery, content),
375 )
376 }
377 mux.HandleFunc(
378 path.Join(repoPath, "contents/descriptions"),
379 jsonHandler(refQuery, descriptionsContent),
380 )
381 t.Cleanup(server.Close)
382 t.Setenv("GITHUB_TOKEN", "fake token")
383 return server
384}
385
386func assertEqualStrings(t *testing.T, want, got string) {
387 t.Helper()

Callers 1

TestUpdateOpenAPIFunction · 0.85

Calls 1

PtrFunction · 0.92

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…