human_readable_bytes_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package config
  2. import (
  3. "testing"
  4. "github.com/dustin/go-humanize"
  5. "github.com/knadh/koanf/parsers/yaml"
  6. "github.com/knadh/koanf/providers/rawbytes"
  7. "github.com/knadh/koanf/v2"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestDefaultFileUploadMaxBytesMatchesHumanize(t *testing.T) {
  12. n, err := humanize.ParseBytes("10 MB")
  13. require.NoError(t, err)
  14. assert.Equal(t, int64(n), int64(DefaultFileUploadMaxBytes))
  15. }
  16. func TestUnmarshalFileUploadsHumanReadableBytes(t *testing.T) {
  17. yamlText := `
  18. fileUploads:
  19. maxBytes: "512 kb"
  20. actions:
  21. - title: x
  22. shell: echo
  23. arguments:
  24. - name: f
  25. type: file_upload
  26. maxUploadBytes: "2 MiB"
  27. `
  28. k := koanf.New(".")
  29. require.NoError(t, k.Load(rawbytes.Provider([]byte(yamlText)), yaml.Parser()))
  30. cfg := &Config{}
  31. require.NoError(t, k.UnmarshalWithConf("", cfg, koanf.UnmarshalConf{
  32. Tag: "koanf",
  33. DecoderConfig: newDefaultUnmarshalDecoderConfig(),
  34. }))
  35. assert.Equal(t, HumanReadableBytes(512000), cfg.FileUploads.MaxBytes)
  36. require.Len(t, cfg.Actions, 1)
  37. require.Len(t, cfg.Actions[0].Arguments, 1)
  38. assert.Equal(t, HumanReadableBytes(2*1024*1024), cfg.Actions[0].Arguments[0].MaxUploadBytes)
  39. }
  40. func TestUnmarshalFileUploadsMaxBytesNumericStillWorks(t *testing.T) {
  41. yamlText := `
  42. fileUploads:
  43. maxBytes: 1048576
  44. `
  45. k := koanf.New(".")
  46. require.NoError(t, k.Load(rawbytes.Provider([]byte(yamlText)), yaml.Parser()))
  47. cfg := &Config{}
  48. require.NoError(t, k.UnmarshalWithConf("", cfg, koanf.UnmarshalConf{
  49. Tag: "koanf",
  50. DecoderConfig: newDefaultUnmarshalDecoderConfig(),
  51. }))
  52. assert.Equal(t, HumanReadableBytes(1048576), cfg.FileUploads.MaxBytes)
  53. }