|
| 1 | +package logrequest |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +func TestToLogger(t *testing.T) { |
| 14 | + tables := []struct { |
| 15 | + statusCode int |
| 16 | + method string |
| 17 | + path string |
| 18 | + expectedStartedResults string |
| 19 | + expectedCompletedResults string |
| 20 | + }{ |
| 21 | + {http.StatusOK, http.MethodGet, "/foo", fmt.Sprintf(`Started %s "%s"`, http.MethodGet, "/foo"), fmt.Sprintf("Completed %d in", http.StatusOK)}, |
| 22 | + {http.StatusUnauthorized, http.MethodPost, "/bar/create", fmt.Sprintf(`Started %s "%s"`, http.MethodPost, "/bar/create"), fmt.Sprintf("Completed %d in", http.StatusUnauthorized)}, |
| 23 | + {http.StatusNotFound, http.MethodGet, "/hello/world", fmt.Sprintf(`Started %s "%s"`, http.MethodGet, "/hello/world"), fmt.Sprintf("Completed %d in", http.StatusNotFound)}, |
| 24 | + {http.StatusInternalServerError, http.MethodGet, "/", fmt.Sprintf(`Started %s "%s"`, http.MethodGet, "/"), fmt.Sprintf("Completed %d in", http.StatusInternalServerError)}, |
| 25 | + {http.StatusServiceUnavailable, http.MethodPut, "/foo/update", fmt.Sprintf(`Started %s "%s"`, http.MethodPut, "/foo/update"), fmt.Sprintf("Completed %d in", http.StatusServiceUnavailable)}, |
| 26 | + } |
| 27 | + |
| 28 | + for _, table := range tables { |
| 29 | + var str bytes.Buffer |
| 30 | + var logger = log.Logger{} |
| 31 | + logger.SetOutput(&str) |
| 32 | + |
| 33 | + app := &application{ |
| 34 | + infoLog: &logger, |
| 35 | + } |
| 36 | + req, err := http.NewRequest(table.method, table.path, nil) |
| 37 | + if err != nil { |
| 38 | + t.Fatal(err) |
| 39 | + } |
| 40 | + |
| 41 | + testHandler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| 42 | + rw.WriteHeader(table.statusCode) |
| 43 | + }) |
| 44 | + |
| 45 | + rr := httptest.NewRecorder() |
| 46 | + handler := app.logRequestToLogger(testHandler) |
| 47 | + handler.ServeHTTP(rr, req) |
| 48 | + |
| 49 | + if strings.Contains(str.String(), table.expectedStartedResults) == false { |
| 50 | + t.Errorf("Expected output was incorrect, %s does not contain %s", str.String(), table.expectedStartedResults) |
| 51 | + } |
| 52 | + |
| 53 | + if strings.Contains(str.String(), table.expectedCompletedResults) == false { |
| 54 | + t.Errorf("Expected output was incorrect, %s does not contain %s", str.String(), table.expectedCompletedResults) |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestToString(t *testing.T) { |
| 60 | + tables := []struct { |
| 61 | + statusCode int |
| 62 | + method string |
| 63 | + path string |
| 64 | + expectedStartedResults string |
| 65 | + expectedCompletedResults string |
| 66 | + }{ |
| 67 | + {http.StatusOK, http.MethodGet, "/foo", fmt.Sprintf(`Started %s "%s"`, http.MethodGet, "/foo"), fmt.Sprintf("Completed %d in", http.StatusOK)}, |
| 68 | + {http.StatusUnauthorized, http.MethodPost, "/bar/create", fmt.Sprintf(`Started %s "%s"`, http.MethodPost, "/bar/create"), fmt.Sprintf("Completed %d in", http.StatusUnauthorized)}, |
| 69 | + {http.StatusNotFound, http.MethodGet, "/hello/world", fmt.Sprintf(`Started %s "%s"`, http.MethodGet, "/hello/world"), fmt.Sprintf("Completed %d in", http.StatusNotFound)}, |
| 70 | + {http.StatusInternalServerError, http.MethodGet, "/", fmt.Sprintf(`Started %s "%s"`, http.MethodGet, "/"), fmt.Sprintf("Completed %d in", http.StatusInternalServerError)}, |
| 71 | + {http.StatusServiceUnavailable, http.MethodPut, "/foo/update", fmt.Sprintf(`Started %s "%s"`, http.MethodPut, "/foo/update"), fmt.Sprintf("Completed %d in", http.StatusServiceUnavailable)}, |
| 72 | + } |
| 73 | + |
| 74 | + for _, table := range tables { |
| 75 | + var str bytes.Buffer |
| 76 | + var logger = log.Logger{} |
| 77 | + logger.SetOutput(&str) |
| 78 | + |
| 79 | + app := &application{ |
| 80 | + infoLog: &logger, |
| 81 | + } |
| 82 | + req, err := http.NewRequest(table.method, table.path, nil) |
| 83 | + if err != nil { |
| 84 | + t.Fatal(err) |
| 85 | + } |
| 86 | + |
| 87 | + testHandler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| 88 | + rw.WriteHeader(table.statusCode) |
| 89 | + }) |
| 90 | + |
| 91 | + rr := httptest.NewRecorder() |
| 92 | + handler := app.logRequestToString(testHandler) |
| 93 | + handler.ServeHTTP(rr, req) |
| 94 | + |
| 95 | + if strings.Contains(str.String(), table.expectedStartedResults) == false { |
| 96 | + t.Errorf("Expected output was incorrect, %s does not contain %s", str.String(), table.expectedStartedResults) |
| 97 | + } |
| 98 | + |
| 99 | + if strings.Contains(str.String(), table.expectedCompletedResults) == false { |
| 100 | + t.Errorf("Expected output was incorrect, %s does not contain %s", str.String(), table.expectedCompletedResults) |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// Helpers |
| 106 | + |
| 107 | +type application struct { |
| 108 | + infoLog *log.Logger |
| 109 | +} |
| 110 | + |
| 111 | +func (app *application) logRequestToLogger(next http.Handler) http.Handler { |
| 112 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 113 | + lr := LogRequest{Request: r, Writer: w, Handler: next} |
| 114 | + lr.ToLogger(app.infoLog) |
| 115 | + }) |
| 116 | +} |
| 117 | + |
| 118 | +func (app *application) logRequestToString(next http.Handler) http.Handler { |
| 119 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 120 | + lr := LogRequest{Request: r, Writer: w, Handler: next} |
| 121 | + app.infoLog.Println(lr.ToString()["started"]) |
| 122 | + app.infoLog.Println(lr.ToString()["completed"]) |
| 123 | + }) |
| 124 | +} |
0 commit comments