-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handwritten elasticsearch router, support gzipped body (#486)
* Handwritten elasticsearch router, support gzipped body * Rename tls package -> xtls
- Loading branch information
1 parent
82333a1
commit e34e03c
Showing
13 changed files
with
357 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package http | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestElasticsearchResponse(t *testing.T) { | ||
t.Parallel() | ||
r := require.New(t) | ||
|
||
p := Plugin{} | ||
|
||
tcs := []struct { | ||
Name string | ||
Handler http.HandlerFunc | ||
Request *http.Request | ||
ExpectedBody []byte | ||
ExpectedCode int | ||
}{ | ||
{ | ||
Name: "info", | ||
Handler: p.serveElasticsearchInfo, | ||
Request: httptest.NewRequest(http.MethodGet, "/", http.NoBody), | ||
ExpectedBody: info, | ||
ExpectedCode: http.StatusOK, | ||
}, | ||
{ | ||
Name: "info head", | ||
Handler: p.serveElasticsearchInfo, | ||
Request: httptest.NewRequest(http.MethodHead, "/", http.NoBody), | ||
ExpectedBody: nil, | ||
ExpectedCode: http.StatusOK, | ||
}, | ||
{ | ||
Name: "xpack", | ||
Handler: p.serveElasticsearchXPack, | ||
Request: httptest.NewRequest(http.MethodGet, "/", http.NoBody), | ||
ExpectedBody: xpack, | ||
ExpectedCode: http.StatusOK, | ||
}, | ||
{ | ||
Name: "license", | ||
Handler: p.serveElasticsearchLicense, | ||
Request: httptest.NewRequest(http.MethodGet, "/", http.NoBody), | ||
ExpectedBody: license, | ||
ExpectedCode: http.StatusOK, | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
tc := tc | ||
t.Run(tc.Name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
rec := httptest.NewRecorder() | ||
|
||
tc.Handler.ServeHTTP(rec, tc.Request) | ||
|
||
r.Equal(http.StatusOK, rec.Code) | ||
|
||
body := rec.Body.Bytes() | ||
r.Equal(tc.ExpectedBody, body) | ||
|
||
// check body is valid json | ||
if len(body) > 0 { | ||
m := map[string]any{} | ||
r.NoError(json.Unmarshal(rec.Body.Bytes(), &m)) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.