diff --git a/internal/wrphandlers/mocktr181/handler.go b/internal/wrphandlers/mocktr181/handler.go index bce8768..fff6f67 100644 --- a/internal/wrphandlers/mocktr181/handler.go +++ b/internal/wrphandlers/mocktr181/handler.go @@ -163,18 +163,24 @@ func (h Handler) get(tr181 *Tr181Payload) (int64, []byte, error) { result := Tr181Payload{ Command: tr181.Command, Names: tr181.Names, - StatusCode: 520, + StatusCode: http.StatusOK, } + var ( + failedNames []string + readableParams []Parameter + ) for _, name := range tr181.Names { + var found bool for _, mockParameter := range h.parameters { if !strings.HasPrefix(mockParameter.Name, name) { continue } - switch mockParameter.Access { - case "r", "rw", "wr": - result.Parameters = append(result.Parameters, Parameter{ + // Check whether mockParameter is readable. + if strings.Contains(mockParameter.Access, "r") { + found = true + readableParams = append(readableParams, Parameter{ Name: mockParameter.Name, Value: mockParameter.Value, DataType: mockParameter.DataType, @@ -182,16 +188,35 @@ func (h Handler) get(tr181 *Tr181Payload) (int64, []byte, error) { Message: "Success", Count: 1, }) - result.StatusCode = http.StatusOK - default: - result.Parameters = append(result.Parameters, Parameter{ - Message: fmt.Sprintf("Invalid parameter name: %s", mockParameter.Name), - }) - result.StatusCode = 520 + continue + } + + // If the requested parameter is a wild card and is not readable, + // then continue and don't count it as a failure. + if name[len(name)-1] == '.' { + continue } + + // mockParameter is not readable. + failedNames = append(failedNames, mockParameter.Name) + } + + if !found { + // Requested parameter was not found. + failedNames = append(failedNames, name) } } + result.Parameters = readableParams + // Check if any parameters failed. + if len(failedNames) != 0 { + // If any names failed, then do not return any parameters that succeeded. + result.Parameters = []Parameter{{ + Message: fmt.Sprintf("Invalid parameter names: %s", failedNames), + }} + result.StatusCode = 520 + } + payload, err := json.Marshal(result) if err != nil { return http.StatusInternalServerError, payload, errors.Join(ErrInvalidResponsePayload, err) diff --git a/internal/wrphandlers/mocktr181/handler_test.go b/internal/wrphandlers/mocktr181/handler_test.go index de3cba4..dbb0815 100644 --- a/internal/wrphandlers/mocktr181/handler_test.go +++ b/internal/wrphandlers/mocktr181/handler_test.go @@ -59,7 +59,7 @@ func TestHandler_HandleWrp(t *testing.T) { var result Tr181Payload err := json.Unmarshal(msg.Payload, &result) a.NoError(err) - a.Equal(0, len(result.Parameters)) + a.Equal(1, len(result.Parameters)) a.True(h.Enabled()) return nil },