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