mirror of
https://github.com/kubescape/kubescape.git
synced 2026-04-15 06:58:11 +00:00
* Interfaces are unchanged
* Deprecated: low-level API funcs marked for deprecation:
HttpPost, HttpGetter, HttpDelete (an augmented version of the KS Cloud
client will expose the post report API, which is currently the sole
use-case of low-level API)
* Doc: the package is now godoc-friendly
* Style & code layout:
* listed all exposed types via aliases, for clarity/less confusing
imports
* unexported private types
* factorized query param logic
* factorized type <-> JSON using generic func & io.Reader
* "utils" are now limited to a few common utility functions
* centralized hard-coded strings as (unexported) constants
* concision: use higher-level http definitions such as constants,
cookie methods, etc
* included type-safety guards to verify that interfaces are
actually implemented by the exported types
* Tests: existing test assertions are unchanged
* tests are beefed-up to assert proper authentication flow (token & cookie).
* added unit tests for utility methods
* Perf:
* unmarshalling API responses is now flowing without extraneous memory allocation via string representation
* request headers are now passed withot extraneous map allocation
* JSON operations are now fully supported by jsoniter (no longer use encoding/json)
* Changes in functionality:
* the client is now fully extensible with KSCloudOption
* use the option functor idiom to keep constructors short
* methods that used to mute errors (i.e. return nil, nil) now bubble up errors
* the captured cookie is now captured in full, not just its value
(other cookie parameters returned are stored)
* added a request/response dump option, for debugging
* added support for SubmitReport and retrieval of UI url's
* backported utm changes (reports use case)
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
98 lines
2.3 KiB
Go
98 lines
2.3 KiB
Go
package getter
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGetDefaultPath(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
const name = "mine"
|
|
|
|
pth := GetDefaultPath(name)
|
|
require.Equal(t, name, filepath.Base(pth))
|
|
require.Equal(t, ".kubescape", filepath.Base(filepath.Dir(pth)))
|
|
}
|
|
|
|
func TestSaveInFile(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
dir, err := os.MkdirTemp(".", "test")
|
|
require.NoError(t, err)
|
|
defer func() {
|
|
_ = os.RemoveAll(dir)
|
|
}()
|
|
|
|
policy := map[string]interface{}{
|
|
"key": "value",
|
|
"number": 1.00,
|
|
}
|
|
|
|
t.Run("should save data as JSON (target folder exists)", func(t *testing.T) {
|
|
target := filepath.Join(dir, "target.json")
|
|
require.NoError(t, SaveInFile(policy, target))
|
|
|
|
buf, err := os.ReadFile(target)
|
|
require.NoError(t, err)
|
|
var retrieved interface{}
|
|
require.NoError(t, json.Unmarshal(buf, &retrieved))
|
|
|
|
require.EqualValues(t, policy, retrieved)
|
|
})
|
|
|
|
t.Run("should save data as JSON (new target folder)", func(t *testing.T) {
|
|
target := filepath.Join(dir, "subdir", "target.json")
|
|
require.NoError(t, SaveInFile(policy, target))
|
|
|
|
buf, err := os.ReadFile(target)
|
|
require.NoError(t, err)
|
|
var retrieved interface{}
|
|
require.NoError(t, json.Unmarshal(buf, &retrieved))
|
|
|
|
require.EqualValues(t, policy, retrieved)
|
|
})
|
|
|
|
t.Run("should error", func(t *testing.T) {
|
|
badPolicy := map[string]interface{}{
|
|
"key": "value",
|
|
"number": 1.00,
|
|
"err": func() {},
|
|
}
|
|
target := filepath.Join(dir, "error.json")
|
|
require.Error(t, SaveInFile(badPolicy, target))
|
|
})
|
|
}
|
|
|
|
func TestHttpMethods(t *testing.T) {
|
|
client := http.DefaultClient
|
|
hdrs := map[string]string{"key": "value"}
|
|
|
|
srv := mockAPIServer(t)
|
|
t.Cleanup(srv.Close)
|
|
|
|
t.Run("HttpGetter should GET", func(t *testing.T) {
|
|
resp, err := HttpGetter(client, srv.URL(pathTestGet), hdrs)
|
|
require.NoError(t, err)
|
|
require.EqualValues(t, "body-get", resp)
|
|
})
|
|
|
|
t.Run("HttpPost should POST", func(t *testing.T) {
|
|
body := []byte("body-post")
|
|
|
|
resp, err := HttpPost(client, srv.URL(pathTestPost), hdrs, body)
|
|
require.NoError(t, err)
|
|
require.EqualValues(t, string(body), resp)
|
|
})
|
|
|
|
t.Run("HttpDelete should DELETE", func(t *testing.T) {
|
|
resp, err := HttpDelete(client, srv.URL(pathTestDelete), hdrs)
|
|
require.NoError(t, err)
|
|
require.EqualValues(t, "body-delete", resp)
|
|
})
|
|
}
|