generated from traefik/plugindemo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtraefik_log_elasticsearch_integration_test.go
67 lines (55 loc) · 1.68 KB
/
traefik_log_elasticsearch_integration_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
64
65
66
67
//go:build integration
// +build integration
package traefiklogelasticsearch_test
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
traefiklogelasticsearch "github.com/cmdbg/traefik-log-elasticsearch-plugin"
)
func TestIntegrationLogElasticsearch(t *testing.T) {
cfg := traefiklogelasticsearch.CreateConfig()
cfg.Message = "Test Elasticsearch"
cfg.ElasticsearchURL = "https://your.elastic.com"
cfg.IndexName = "test-index"
cfg.Username = "elastic"
cfg.Password = "ff9fKJta3Zb30E8re21I5043"
cfg.VerifyTLS = true
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)
}
})
elasticsearchLog := &traefiklogelasticsearch.ElasticsearchLog{
Next: next,
Name: "test",
Message: cfg.Message,
ElasticsearchURL: cfg.ElasticsearchURL,
IndexName: cfg.IndexName,
Username: cfg.Username,
Password: cfg.Password,
VerifyTLS: cfg.VerifyTLS,
}
req := httptest.NewRequest(http.MethodGet, "http://test.com/foo", nil)
w := httptest.NewRecorder()
elasticsearchLog.ServeHTTP(w, req)
resp := w.Result()
if resp.StatusCode != http.StatusOK {
t.Fatalf("expected status 200, got %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
defer func() {
err = resp.Body.Close()
if err != nil {
t.Fatalf("Error closing the response body: %s", err)
}
}()
if err != nil {
t.Fatalf("Could not read response: %v", err)
}
if string(body) != "next handler" {
t.Errorf("Handler did not chain to the next middleware. Got: %s", body)
}
}