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
  • Loading branch information
denopink committed Jul 15, 2024
1 parent 7e28f22 commit a791a90
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions internal/wrphandlers/mocktr181/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,35 +162,46 @@ 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
params []Parameter
)
for _, name := range tr181.Names {
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") {
params = append(params, 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)
}
}

result.Parameters = params
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

0 comments on commit a791a90

Please sign in to comment.