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

Function TestNewRequest

github/github_test.go:1553–1593  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1551}
1552
1553func TestNewRequest(t *testing.T) {
1554 t.Parallel()
1555 c := mustNewClient(t)
1556
1557 inURL, outURL := "/foo", defaultBaseURL+"foo"
1558 inBody, outBody := &User{Login: Ptr("l")}, `{"login":"l"}`+"\n"
1559 req, _ := c.NewRequest(t.Context(), "GET", inURL, inBody)
1560
1561 // test that relative URL was expanded
1562 if got, want := req.URL.String(), outURL; got != want {
1563 t.Errorf("NewRequest(%q) URL is %v, want %v", inURL, got, want)
1564 }
1565
1566 // test that body was JSON encoded
1567 body, _ := io.ReadAll(req.Body)
1568 if got, want := string(body), outBody; got != want {
1569 t.Errorf("NewRequest(%q) Body is %v, want %v", inBody, got, want)
1570 }
1571
1572 userAgent := req.Header.Get("User-Agent")
1573
1574 // test that default user-agent is attached to the request
1575 if got, want := userAgent, c.userAgent; got != want {
1576 t.Errorf("NewRequest() User-Agent is %v, want %v", got, want)
1577 }
1578
1579 if !strings.Contains(userAgent, Version) {
1580 t.Errorf("NewRequest() User-Agent should contain %v, found %v", Version, userAgent)
1581 }
1582
1583 apiVersion := req.Header.Get(headerAPIVersion)
1584 if got, want := apiVersion, defaultAPIVersion; got != want {
1585 t.Errorf("NewRequest() %v header is %v, want %v", headerAPIVersion, got, want)
1586 }
1587
1588 req, _ = c.NewRequest(t.Context(), "GET", inURL, inBody, WithVersion("2022-11-29"))
1589 apiVersion = req.Header.Get(headerAPIVersion)
1590 if got, want := apiVersion, "2022-11-29"; got != want {
1591 t.Errorf("NewRequest() %v header is %v, want %v", headerAPIVersion, got, want)
1592 }
1593}
1594
1595func TestNewRequest_invalidJSON(t *testing.T) {
1596 t.Parallel()

Callers

nothing calls this directly

Calls 6

mustNewClientFunction · 0.85
WithVersionFunction · 0.85
NewRequestMethod · 0.80
PtrFunction · 0.70
StringMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…