|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
4 | 7 | "testing" |
5 | 8 |
|
| 9 | + "github.com/jjuarez/dagger-golang-example/internal/greeting" |
6 | 10 | "github.com/stretchr/testify/assert" |
7 | 11 | ) |
8 | 12 |
|
9 | | -func TestMain(t *testing.T) { |
| 13 | +func TestWithoutName(t *testing.T) { |
10 | 14 | t.Run("your real tests should go here", func(t *testing.T) { |
11 | | - assert.True(t, true, "This's just a placeholder") |
| 15 | + testRouter := setupRouter() |
| 16 | + record := httptest.NewRecorder() |
| 17 | + req, _ := http.NewRequest("GET", "/sayhi", nil) |
| 18 | + want := fmt.Sprintf("{\"message\":\"Hi, %s!\"}", greeting.DefaultName) |
| 19 | + testRouter.ServeHTTP(record, req) |
| 20 | + |
| 21 | + assert.Equal(t, http.StatusOK, record.Code) |
| 22 | + assert.Equal(t, want, record.Body.String()) |
| 23 | + }) |
| 24 | +} |
| 25 | + |
| 26 | +func TestWithName(t *testing.T) { |
| 27 | + t.Run("your real tests should go here", func(t *testing.T) { |
| 28 | + testRouter := setupRouter() |
| 29 | + record := httptest.NewRecorder() |
| 30 | + req, _ := http.NewRequest("GET", "/sayhi/testing", nil) |
| 31 | + want := "{\"message\":\"Hi, testing!\"}" |
| 32 | + testRouter.ServeHTTP(record, req) |
| 33 | + |
| 34 | + assert.Equal(t, http.StatusOK, record.Code) |
| 35 | + assert.Equal(t, want, record.Body.String()) |
12 | 36 | }) |
13 | 37 | } |
0 commit comments