|
| 1 | +// Copyright 2022 Flamego. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package hcaptcha |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "io" |
| 10 | + "net/http" |
| 11 | + "net/http/httptest" |
| 12 | + "net/url" |
| 13 | + "strings" |
| 14 | + "testing" |
| 15 | + |
| 16 | + "github.com/flamego/flamego" |
| 17 | + "github.com/stretchr/testify/assert" |
| 18 | + "github.com/stretchr/testify/require" |
| 19 | +) |
| 20 | + |
| 21 | +type mockRoundTripper struct { |
| 22 | + roundTrip func(*http.Request) (*http.Response, error) |
| 23 | +} |
| 24 | + |
| 25 | +func (m *mockRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) { |
| 26 | + return m.roundTrip(r) |
| 27 | +} |
| 28 | + |
| 29 | +func TestCaptcha(t *testing.T) { |
| 30 | + tests := []struct { |
| 31 | + name string |
| 32 | + wantSecret string |
| 33 | + wantToken string |
| 34 | + wantRemoteIP string |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: "normal", |
| 38 | + wantSecret: "test-secret", |
| 39 | + wantToken: "valid-token", |
| 40 | + wantRemoteIP: "", |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "normal", |
| 44 | + wantSecret: "test-secret", |
| 45 | + wantToken: "valid-token", |
| 46 | + wantRemoteIP: "127.0.0.1", |
| 47 | + }, |
| 48 | + } |
| 49 | + for _, test := range tests { |
| 50 | + t.Run(test.name, func(t *testing.T) { |
| 51 | + client := &http.Client{ |
| 52 | + Transport: &mockRoundTripper{ |
| 53 | + roundTrip: func(r *http.Request) (*http.Response, error) { |
| 54 | + assert.Equal(t, test.wantSecret, r.PostFormValue("secret")) |
| 55 | + assert.Equal(t, test.wantToken, r.PostFormValue("response")) |
| 56 | + assert.Equal(t, test.wantRemoteIP, r.PostFormValue("remoteip")) |
| 57 | + return &http.Response{ |
| 58 | + StatusCode: http.StatusOK, |
| 59 | + Body: io.NopCloser(strings.NewReader(`{"success": true}`)), |
| 60 | + Request: r, |
| 61 | + }, nil |
| 62 | + }, |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + f := flamego.NewWithLogger(&bytes.Buffer{}) |
| 67 | + f.Use(Captcha(Options{ |
| 68 | + Client: client, |
| 69 | + Secret: test.wantSecret, |
| 70 | + })) |
| 71 | + f.Post("/", func(r *http.Request, h HCaptcha) { |
| 72 | + token := r.PostFormValue("h-captcha-response") |
| 73 | + |
| 74 | + var err error |
| 75 | + var resp *Response |
| 76 | + if test.wantRemoteIP != "" { |
| 77 | + resp, err = h.Verify(token, test.wantRemoteIP) |
| 78 | + } else { |
| 79 | + resp, err = h.Verify(token) |
| 80 | + } |
| 81 | + require.NoError(t, err) |
| 82 | + assert.True(t, resp.Success) |
| 83 | + }) |
| 84 | + |
| 85 | + resp := httptest.NewRecorder() |
| 86 | + req, err := http.NewRequest(http.MethodPost, "/", nil) |
| 87 | + require.NoError(t, err) |
| 88 | + |
| 89 | + req.PostForm = url.Values{ |
| 90 | + "h-captcha-response": {test.wantToken}, |
| 91 | + } |
| 92 | + f.ServeHTTP(resp, req) |
| 93 | + }) |
| 94 | + } |
| 95 | +} |
0 commit comments