| 1955 | } |
| 1956 | |
| 1957 | func Test_apiRun_acceptHeader(t *testing.T) { |
| 1958 | tests := []struct { |
| 1959 | name string |
| 1960 | options ApiOptions |
| 1961 | wantAcceptHeader string |
| 1962 | }{ |
| 1963 | { |
| 1964 | name: "sets default accept header", |
| 1965 | options: ApiOptions{}, |
| 1966 | wantAcceptHeader: "*/*", |
| 1967 | }, |
| 1968 | { |
| 1969 | name: "does not override user accept header", |
| 1970 | options: ApiOptions{ |
| 1971 | RequestHeaders: []string{"Accept: testing"}, |
| 1972 | }, |
| 1973 | wantAcceptHeader: "testing", |
| 1974 | }, |
| 1975 | { |
| 1976 | name: "does not override preview names", |
| 1977 | options: ApiOptions{ |
| 1978 | Previews: []string{"nebula"}, |
| 1979 | }, |
| 1980 | wantAcceptHeader: "application/vnd.github.nebula-preview+json", |
| 1981 | }, |
| 1982 | } |
| 1983 | for _, tt := range tests { |
| 1984 | t.Run(tt.name, func(t *testing.T) { |
| 1985 | ios, _, _, _ := iostreams.Test() |
| 1986 | tt.options.IO = ios |
| 1987 | |
| 1988 | tt.options.Config = func() (gh.Config, error) { |
| 1989 | return config.NewMockConfig(), nil |
| 1990 | } |
| 1991 | |
| 1992 | var gotReq *http.Request |
| 1993 | tt.options.HttpClient = func(rootapi.HTTPClientOptions) (*http.Client, error) { |
| 1994 | var tr roundTripper = func(req *http.Request) (*http.Response, error) { |
| 1995 | gotReq = req |
| 1996 | resp := &http.Response{ |
| 1997 | StatusCode: 200, |
| 1998 | Request: req, |
| 1999 | Body: io.NopCloser(bytes.NewBufferString("")), |
| 2000 | } |
| 2001 | return resp, nil |
| 2002 | } |
| 2003 | return &http.Client{Transport: tr}, nil |
| 2004 | } |
| 2005 | |
| 2006 | assert.NoError(t, apiRun(&tt.options)) |
| 2007 | assert.Equal(t, tt.wantAcceptHeader, gotReq.Header.Get("Accept")) |
| 2008 | }) |
| 2009 | } |
| 2010 | } |