|
| 1 | +package httptimeoutattr_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + . "github.com/onsi/ginkgo/v2" |
| 7 | + . "github.com/onsi/gomega" |
| 8 | + |
| 9 | + "ocm.software/ocm/api/datacontext" |
| 10 | + "ocm.software/ocm/api/datacontext/attrs/httptimeoutattr" |
| 11 | + "ocm.software/ocm/api/utils/runtime" |
| 12 | +) |
| 13 | + |
| 14 | +var _ = Describe("httptimeout attribute", func() { |
| 15 | + var ctx datacontext.Context |
| 16 | + attr := httptimeoutattr.AttributeType{} |
| 17 | + enc := runtime.DefaultJSONEncoding |
| 18 | + |
| 19 | + BeforeEach(func() { |
| 20 | + ctx = datacontext.New(nil) |
| 21 | + }) |
| 22 | + |
| 23 | + Context("get and set", func() { |
| 24 | + It("defaults to DefaultTimeout", func() { |
| 25 | + Expect(httptimeoutattr.Get(ctx)).To(Equal(httptimeoutattr.DefaultTimeout)) |
| 26 | + }) |
| 27 | + |
| 28 | + It("sets and retrieves duration", func() { |
| 29 | + Expect(httptimeoutattr.Set(ctx, 5*time.Minute)).To(Succeed()) |
| 30 | + Expect(httptimeoutattr.Get(ctx)).To(Equal(5 * time.Minute)) |
| 31 | + |
| 32 | + Expect(httptimeoutattr.Set(ctx, 2*time.Minute)).To(Succeed()) |
| 33 | + Expect(httptimeoutattr.Get(ctx)).To(Equal(2 * time.Minute)) |
| 34 | + }) |
| 35 | + }) |
| 36 | + |
| 37 | + Context("encoding values to JSON", func() { |
| 38 | + DescribeTable("encodes valid input", |
| 39 | + func(input interface{}, expected string) { |
| 40 | + data, err := attr.Encode(input, enc) |
| 41 | + Expect(err).To(Succeed()) |
| 42 | + Expect(string(data)).To(Equal(expected)) |
| 43 | + }, |
| 44 | + Entry("time.Duration 30s to JSON string", 30*time.Second, `"30s"`), |
| 45 | + Entry("duration string 5m to JSON string", "5m", `"5m"`), |
| 46 | + ) |
| 47 | + |
| 48 | + DescribeTable("rejects invalid input", |
| 49 | + func(input interface{}, errSubstring string) { |
| 50 | + _, err := attr.Encode(input, enc) |
| 51 | + Expect(err).To(HaveOccurred()) |
| 52 | + Expect(err.Error()).To(ContainSubstring(errSubstring)) |
| 53 | + }, |
| 54 | + Entry("non-parseable string like notaduration", "notaduration", "invalid duration string"), |
| 55 | + Entry("string with unknown unit like 1Gb", "1Gb", "invalid duration string"), |
| 56 | + Entry("unsupported type like bool", true, "duration or duration string required"), |
| 57 | + ) |
| 58 | + }) |
| 59 | + |
| 60 | + Context("decoding values from JSON", func() { |
| 61 | + DescribeTable("decodes valid JSON input", |
| 62 | + func(input string, expected time.Duration) { |
| 63 | + val, err := attr.Decode([]byte(input), enc) |
| 64 | + Expect(err).To(Succeed()) |
| 65 | + Expect(val).To(Equal(expected)) |
| 66 | + }, |
| 67 | + Entry("duration string 30s", `"30s"`, 30*time.Second), |
| 68 | + Entry("duration string 5m", `"5m"`, 5*time.Minute), |
| 69 | + Entry("duration string 1h", `"1h"`, 1*time.Hour), |
| 70 | + Entry("nanoseconds number 300000000000 as 5m", `300000000000`, 5*time.Minute), |
| 71 | + ) |
| 72 | + |
| 73 | + DescribeTable("rejects invalid JSON input", |
| 74 | + func(input string, errSubstring string) { |
| 75 | + _, err := attr.Decode([]byte(input), enc) |
| 76 | + Expect(err).To(HaveOccurred()) |
| 77 | + Expect(err.Error()).To(ContainSubstring(errSubstring)) |
| 78 | + }, |
| 79 | + Entry("non-parseable string like notaduration", `"notaduration"`, "invalid timeout value"), |
| 80 | + Entry("string with unknown unit like 1Gb", `"1Gb"`, "invalid timeout value"), |
| 81 | + Entry("digit-only string like 300000000000", `"300000000000"`, "invalid timeout value"), |
| 82 | + Entry("JSON boolean true", `true`, "must be a duration string or nanoseconds number"), |
| 83 | + ) |
| 84 | + }) |
| 85 | +}) |
0 commit comments