Skip to content

Commit

Permalink
patch: update mock181's get, check for non-readable parameters
Browse files Browse the repository at this point in the history
During mock181's set:
- check for any non-readable params
- if any param is not readable, do not return any parameters that succeeded
- add case when request param is not found
  • Loading branch information
denopink committed Jul 16, 2024
1 parent 7e28f22 commit 1584ec2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
39 changes: 29 additions & 10 deletions internal/wrphandlers/mocktr181/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,35 +162,54 @@ 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,
Attributes: mockParameter.Attributes,
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
}

// 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)
Expand Down
2 changes: 1 addition & 1 deletion internal/wrphandlers/mocktr181/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down

0 comments on commit 1584ec2

Please sign in to comment.