Skip to content

Commit

Permalink
patch: add statusCode to mocktr181's response payload
Browse files Browse the repository at this point in the history
- add the missing `statusCode` field to mocktr181's response payload
  • Loading branch information
denopink committed Jul 8, 2024
1 parent af06700 commit 24cf454
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions internal/wrphandlers/mocktr181/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Tr181Payload struct {
Command string `json:"command"`
Names []string `json:"names"`
Parameters []Parameter `json:"parameters"`
StatusCode int `json:"statusCode"`
}

type Parameters struct {
Expand Down Expand Up @@ -157,8 +158,9 @@ func (h Handler) HandleWrp(msg wrp.Message) error {
}

func (h Handler) get(names []string) (int64, []byte, error) {
result := Tr181Payload{}
statusCode := int64(http.StatusOK)
result := Tr181Payload{
StatusCode: http.StatusOK,
}

for _, name := range names {
for _, mockParameter := range h.parameters {
Expand All @@ -178,26 +180,27 @@ func (h Handler) get(names []string) (int64, []byte, error) {
result.Parameters = append(result.Parameters, Parameter{
Message: fmt.Sprintf("Invalid parameter name: %s", mockParameter.Name),
})
statusCode = 520
result.StatusCode = 520
}
}
}

if len(result.Parameters) == 0 {
result.StatusCode = 520
}

payload, err := json.Marshal(result)
if err != nil {
return http.StatusInternalServerError, payload, errors.Join(ErrInvalidResponsePayload, err)
}

if len(result.Parameters) == 0 {
statusCode = int64(520)
}

return statusCode, payload, nil
return int64(result.StatusCode), payload, nil
}

func (h Handler) set(parameters []Parameter) (int64, []byte, error) {
statusCode := http.StatusAccepted
result := Tr181Payload{}
result := Tr181Payload{
StatusCode: http.StatusAccepted,
}
for _, parameter := range parameters {
for i := range h.parameters {
mockParameter := &h.parameters[i]
Expand All @@ -222,7 +225,7 @@ func (h Handler) set(parameters []Parameter) (int64, []byte, error) {
Name: mockParameter.Name,
Message: "Parameter is not writable",
})
statusCode = 520
result.StatusCode = 520
}
}
}
Expand All @@ -232,7 +235,7 @@ func (h Handler) set(parameters []Parameter) (int64, []byte, error) {
return http.StatusInternalServerError, payload, errors.Join(ErrInvalidResponsePayload, err)
}

return int64(statusCode), payload, nil
return int64(result.StatusCode), payload, nil
}

func (h Handler) loadFile() ([]MockParameter, error) {
Expand Down

0 comments on commit 24cf454

Please sign in to comment.