package proxy import ( "encoding/base64" "fmt" "io/ioutil" "net/http" "net/http/httptest" "strconv" "strings" "testing" "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestPicture_Extract(t *testing.T) { tbl := []struct { inp string res []string }{ { `

blah test

`, []string{"http://radio-t.com/img.png"}, }, { `

blah test

`, []string{}, }, { ``, []string{"http://radio-t.com/img2.png"}, }, { `
xyz
`, []string{"http://radio-t.com/img3.png", "http://images.pexels.com/67636/img4.jpeg"}, }, { `
xyz
`, []string{"http://images.pexels.com/67636/img4.jpeg"}, }, { `abcd blah

xxx

`, []string{}, }, } img := Image{Enabled: true} for i, tt := range tbl { t.Run(strconv.Itoa(i), func(t *testing.T) { res, err := img.extract(tt.inp) assert.Nil(t, err) assert.Equal(t, tt.res, res) }) } } func TestPicture_Replace(t *testing.T) { img := Image{Enabled: true, RoutePath: "/img"} r := img.replace(` xyz `, []string{"http://radio-t.com/img3.png", "http://images.pexels.com/67636/img4.jpeg"}) assert.Equal(t, ` xyz `, r) } func TestImage_Routes(t *testing.T) { img := Image{Enabled: true, RemarkURL: "https://demo.remark42.com", RoutePath: "/api/v1/proxy"} ts := httptest.NewServer(http.HandlerFunc(img.Handler)) defer ts.Close() httpSrv := imgHTTPServer(t) defer httpSrv.Close() encodedImgURL := base64.URLEncoding.EncodeToString([]byte(httpSrv.URL + "/image/img1.png")) resp, err := http.Get(ts.URL + "/?src=" + encodedImgURL) require.Nil(t, err) assert.Equal(t, 200, resp.StatusCode) t.Logf("%+v", resp.Header) assert.Equal(t, "123", resp.Header["Content-Length"][0]) assert.Equal(t, "image/png", resp.Header["Content-Type"][0]) encodedImgURL = base64.URLEncoding.EncodeToString([]byte(httpSrv.URL + "/image/no-such-image.png")) resp, err = http.Get(ts.URL + "/?src=" + encodedImgURL) require.Nil(t, err) assert.Equal(t, 404, resp.StatusCode) encodedImgURL = base64.URLEncoding.EncodeToString([]byte(httpSrv.URL + "bad encoding")) resp, err = http.Get(ts.URL + "/?src=" + encodedImgURL) require.Nil(t, err) assert.Equal(t, 400, resp.StatusCode) } func TestImage_RoutesTimedOut(t *testing.T) { img := Image{Enabled: true, RemarkURL: "https://demo.remark42.com", RoutePath: "/api/v1/proxy", Timeout: 50 * time.Millisecond} ts := httptest.NewServer(http.HandlerFunc(img.Handler)) defer ts.Close() httpSrv := imgHTTPServer(t) defer httpSrv.Close() encodedImgURL := base64.URLEncoding.EncodeToString([]byte(httpSrv.URL + "/image/img-slow.png")) resp, err := http.Get(ts.URL + "/?src=" + encodedImgURL) require.Nil(t, err) assert.Equal(t, 400, resp.StatusCode) b, err := ioutil.ReadAll(resp.Body) require.Nil(t, err) t.Log(string(b)) assert.True(t, strings.Contains(string(b), "deadline exceeded")) } func TestPicture_Convert(t *testing.T) { img := Image{Enabled: true, RoutePath: "/img"} r := img.Convert(` xyz `) assert.Equal(t, ` xyz `, r) r = img.Convert(` xyz `) assert.Equal(t, ` xyz `, r) img = Image{Enabled: true, RoutePath: "/img", RemarkURL: "http://example.com"} r = img.Convert(` xyz`) assert.Equal(t, ` xyz`, r, "http:// remark url, no proxy") img = Image{Enabled: false, RoutePath: "/img"} r = img.Convert(` xyz`) assert.Equal(t, ` xyz`, r, "disabled, no proxy") } func imgHTTPServer(t *testing.T) *httptest.Server { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/image/img1.png" { t.Log("http img request", r.URL) w.Header().Add("Content-Length", "123") w.Header().Add("Content-Type", "image/png") _, err := w.Write([]byte(fmt.Sprintf("%123s", "X"))) assert.NoError(t, err) return } if r.URL.Path == "/image/img-slow.png" { time.Sleep(500 * time.Millisecond) w.WriteHeader(500) return } t.Log("http img request - not found", r.URL) w.WriteHeader(404) })) return ts }