8
8
"net/http/httptest"
9
9
"strings"
10
10
"testing"
11
+ "time"
11
12
)
12
13
13
14
func TestToLogger (t * testing.T ) {
@@ -56,6 +57,38 @@ func TestToLogger(t *testing.T) {
56
57
}
57
58
}
58
59
60
+ func TestToLoggerWithOptionals (t * testing.T ) {
61
+ var str bytes.Buffer
62
+ var logger = log.Logger {}
63
+ logger .SetOutput (& str )
64
+
65
+ app := & application {
66
+ infoLog : & logger ,
67
+ }
68
+ req , err := http .NewRequest (http .MethodGet , "/foo" , nil )
69
+ if err != nil {
70
+ t .Fatal (err )
71
+ }
72
+
73
+ testHandler := http .HandlerFunc (func (rw http.ResponseWriter , r * http.Request ) {
74
+ rw .WriteHeader (http .StatusOK )
75
+ })
76
+
77
+ rr := httptest .NewRecorder ()
78
+ handler := app .logRequestToLoggerWithOptionals (testHandler )
79
+ handler .ServeHTTP (rr , req )
80
+
81
+ expectedStartedResults := fmt .Sprintf (`at %s` , time .Now ().Format ("2006-01-02 15:04:05" ))
82
+ if strings .Contains (str .String (), expectedStartedResults ) == false {
83
+ t .Errorf ("Expected output was incorrect, %s does not contain %s" , str .String (), expectedStartedResults )
84
+ }
85
+
86
+ expectedCompletedResults := fmt .Sprintln ("\t " )
87
+ if strings .Contains (str .String (), expectedCompletedResults ) == false {
88
+ t .Errorf ("Expected output was incorrect, %s does not contain new line" , str .String ())
89
+ }
90
+ }
91
+
59
92
func TestToString (t * testing.T ) {
60
93
tables := []struct {
61
94
statusCode int
@@ -102,6 +135,33 @@ func TestToString(t *testing.T) {
102
135
}
103
136
}
104
137
138
+ func TestToStringWithOptionals (t * testing.T ) {
139
+ var str bytes.Buffer
140
+ var logger = log.Logger {}
141
+ logger .SetOutput (& str )
142
+
143
+ app := & application {
144
+ infoLog : & logger ,
145
+ }
146
+ req , err := http .NewRequest (http .MethodGet , "/foo" , nil )
147
+ if err != nil {
148
+ t .Fatal (err )
149
+ }
150
+
151
+ testHandler := http .HandlerFunc (func (rw http.ResponseWriter , r * http.Request ) {
152
+ rw .WriteHeader (http .StatusOK )
153
+ })
154
+
155
+ rr := httptest .NewRecorder ()
156
+ handler := app .logRequestToStringWithOptionals (testHandler )
157
+ handler .ServeHTTP (rr , req )
158
+
159
+ expectedStartedResults := fmt .Sprintf (`at %s` , time .Now ().Format ("2006-01-02 15:04:05" ))
160
+ if strings .Contains (str .String (), expectedStartedResults ) == false {
161
+ t .Errorf ("Expected output was incorrect, %s does not contain %s" , str .String (), expectedStartedResults )
162
+ }
163
+ }
164
+
105
165
// Helpers
106
166
107
167
type application struct {
@@ -122,3 +182,18 @@ func (app *application) logRequestToString(next http.Handler) http.Handler {
122
182
app .infoLog .Println (lr .ToString ()["completed" ])
123
183
})
124
184
}
185
+
186
+ func (app * application ) logRequestToLoggerWithOptionals (next http.Handler ) http.Handler {
187
+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
188
+ lr := LogRequest {Request : r , Writer : w , Handler : next , NewLine : 1 , Timestamp : true }
189
+ lr .ToLogger (app .infoLog )
190
+ })
191
+ }
192
+
193
+ func (app * application ) logRequestToStringWithOptionals (next http.Handler ) http.Handler {
194
+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
195
+ lr := LogRequest {Request : r , Writer : w , Handler : next , Timestamp : true }
196
+ app .infoLog .Println (lr .ToString ()["started" ])
197
+ app .infoLog .Println (lr .ToString ()["completed" ])
198
+ })
199
+ }
0 commit comments