| 1886 | } |
| 1887 | |
| 1888 | func Test_apiRun_acceptHeader(t *testing.T) { |
| 1889 | tests := []struct { |
| 1890 | name string |
| 1891 | options ApiOptions |
| 1892 | wantAcceptHeader string |
| 1893 | }{ |
| 1894 | { |
| 1895 | name: "sets default accept header", |
| 1896 | options: ApiOptions{}, |
| 1897 | wantAcceptHeader: "*/*", |
| 1898 | }, |
| 1899 | { |
| 1900 | name: "does not override user accept header", |
| 1901 | options: ApiOptions{ |
| 1902 | RequestHeaders: []string{"Accept: testing"}, |
| 1903 | }, |
| 1904 | wantAcceptHeader: "testing", |
| 1905 | }, |
| 1906 | { |
| 1907 | name: "does not override preview names", |
| 1908 | options: ApiOptions{ |
| 1909 | Previews: []string{"nebula"}, |
| 1910 | }, |
| 1911 | wantAcceptHeader: "application/vnd.github.nebula-preview+json", |
| 1912 | }, |
| 1913 | } |
| 1914 | for _, tt := range tests { |
| 1915 | t.Run(tt.name, func(t *testing.T) { |
| 1916 | ios, _, _, _ := iostreams.Test() |
| 1917 | tt.options.IO = ios |
| 1918 | |
| 1919 | tt.options.Config = func() (gh.Config, error) { |
| 1920 | return config.NewMockConfig(), nil |
| 1921 | } |
| 1922 | |
| 1923 | var gotReq *http.Request |
| 1924 | tt.options.HttpClient = func(rootapi.HTTPClientOptions) (*http.Client, error) { |
| 1925 | var tr roundTripper = func(req *http.Request) (*http.Response, error) { |
| 1926 | gotReq = req |
| 1927 | resp := &http.Response{ |
| 1928 | StatusCode: 200, |
| 1929 | Request: req, |
| 1930 | Body: io.NopCloser(bytes.NewBufferString("")), |
| 1931 | } |
| 1932 | return resp, nil |
| 1933 | } |
| 1934 | return &http.Client{Transport: tr}, nil |
| 1935 | } |
| 1936 | |
| 1937 | assert.NoError(t, apiRun(&tt.options)) |
| 1938 | assert.Equal(t, tt.wantAcceptHeader, gotReq.Header.Get("Accept")) |
| 1939 | }) |
| 1940 | } |
| 1941 | } |