generated from traefik/plugindemo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtraefik_log_elasticsearch_test.go
63 lines (48 loc) · 1.57 KB
/
traefik_log_elasticsearch_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//go:build !generated
// +build !generated
package traefiklogelasticsearch_test
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
traefiklogelasticsearch "github.com/cmdbg/traefik-log-elasticsearch-plugin"
)
func TestLogElasticsearch(t *testing.T) {
// Load configuration from environment variables or use default values
cfg := loadConfig()
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, err := w.Write([]byte("next handler")); err != nil {
http.Error(w, fmt.Sprintf("Error writing response: %v", err), http.StatusInternalServerError)
}
})
handler := logElasticsearch(next, cfg)
req := httptest.NewRequest(http.MethodGet, "http://test.com/foo", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
resp := w.Result()
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected status 200, got %d", resp.StatusCode)
}
body := w.Body.String()
if body != "next handler" {
t.Errorf("Handler did not chain to the next middleware. Got: %s", body)
}
}
func loadConfig() *traefiklogelasticsearch.Config {
cfg := traefiklogelasticsearch.CreateConfig()
cfg.Message = "Test Elasticsearch"
cfg.ElasticsearchURL = "http://localhost:9200"
cfg.IndexName = "test-index"
cfg.Username = "elastic"
cfg.Password = "elastic"
return cfg
}
func logElasticsearch(next http.Handler, _ *traefiklogelasticsearch.Config) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Log the request details here if needed
// ...
// Call the next handler in the middleware chain
next.ServeHTTP(w, r)
})
}