| 10 | ) |
| 11 | |
| 12 | func TestHTTPSSEExprValidation(t *testing.T) { |
| 13 | cases := map[string]struct { |
| 14 | SSE *expr.HTTPSSEExpr |
| 15 | Payload *expr.AttributeExpr |
| 16 | Result *expr.AttributeExpr |
| 17 | ExpectedErrs []string |
| 18 | }{ |
| 19 | "valid-empty": { |
| 20 | SSE: &expr.HTTPSSEExpr{}, |
| 21 | Payload: &expr.AttributeExpr{Type: &expr.Object{}}, |
| 22 | Result: &expr.AttributeExpr{Type: &expr.Object{}}, |
| 23 | }, |
| 24 | "valid-with-fields": { |
| 25 | SSE: &expr.HTTPSSEExpr{ |
| 26 | RequestIDField: "request_id", |
| 27 | DataField: "data", |
| 28 | IDField: "id", |
| 29 | EventField: "event", |
| 30 | RetryField: "retry", |
| 31 | }, |
| 32 | Payload: &expr.AttributeExpr{ |
| 33 | Type: &expr.Object{ |
| 34 | &expr.NamedAttributeExpr{Name: "request_id", Attribute: &expr.AttributeExpr{Type: expr.String}}, |
| 35 | }, |
| 36 | }, |
| 37 | Result: &expr.AttributeExpr{ |
| 38 | Type: &expr.Object{ |
| 39 | &expr.NamedAttributeExpr{Name: "data", Attribute: &expr.AttributeExpr{Type: expr.String}}, |
| 40 | &expr.NamedAttributeExpr{Name: "id", Attribute: &expr.AttributeExpr{Type: expr.String}}, |
| 41 | &expr.NamedAttributeExpr{Name: "event", Attribute: &expr.AttributeExpr{Type: expr.String}}, |
| 42 | &expr.NamedAttributeExpr{Name: "retry", Attribute: &expr.AttributeExpr{Type: expr.Int}}, |
| 43 | }, |
| 44 | }, |
| 45 | }, |
| 46 | "invalid-id-field-type": { |
| 47 | SSE: &expr.HTTPSSEExpr{ |
| 48 | IDField: "id", |
| 49 | }, |
| 50 | Payload: &expr.AttributeExpr{Type: &expr.Object{}}, |
| 51 | Result: &expr.AttributeExpr{ |
| 52 | Type: &expr.Object{ |
| 53 | &expr.NamedAttributeExpr{Name: "id", Attribute: &expr.AttributeExpr{Type: expr.Int}}, |
| 54 | }, |
| 55 | }, |
| 56 | ExpectedErrs: []string{"cannot use \"id\" for SSE event id field: attribute type must be one of string"}, |
| 57 | }, |
| 58 | "invalid-request-id-field-type": { |
| 59 | SSE: &expr.HTTPSSEExpr{ |
| 60 | RequestIDField: "request_id", |
| 61 | }, |
| 62 | Payload: &expr.AttributeExpr{ |
| 63 | Type: &expr.Object{ |
| 64 | &expr.NamedAttributeExpr{Name: "request_id", Attribute: &expr.AttributeExpr{Type: expr.Int}}, |
| 65 | }, |
| 66 | }, |
| 67 | Result: &expr.AttributeExpr{Type: &expr.Object{}}, |
| 68 | ExpectedErrs: []string{"cannot use \"request_id\" for SSE request ID field: attribute type must be one of string"}, |
| 69 | }, |