Skip to content

Commit

Permalink
Merge pull request #111 from pf-lin/fix/rsp-type
Browse files Browse the repository at this point in the history
fix: error response type
  • Loading branch information
ianchen0119 authored Jul 17, 2024
2 parents 9fef3b9 + b77b2ea commit ae96ce5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
38 changes: 27 additions & 11 deletions internal/sbi/processor/pdu_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (p *Processor) HandlePDUSessionSMContextCreate(
Error: &Nsmf_PDUSession.N1SmError,
},
}
c.JSON(http.StatusForbidden, postSmContextsError)
c.Render(http.StatusForbidden, openapi.MultipartRelatedRender{Data: postSmContextsError})
return
}

Expand Down Expand Up @@ -239,7 +239,7 @@ func (p *Processor) HandlePDUSessionSMContextCreate(

response.JsonData = smContext.BuildCreatedData()
c.Header("Location", smContext.Ref)
c.JSON(http.StatusCreated, response)
c.Render(http.StatusCreated, openapi.MultipartRelatedRender{Data: response})
}

func (p *Processor) HandlePDUSessionSMContextUpdate(
Expand Down Expand Up @@ -403,7 +403,7 @@ func (p *Processor) HandlePDUSessionSMContextUpdate(
}

response.JsonData.N1SmMsg = &models.RefToBinaryData{ContentId: "PDUSessionModificationReject"}
c.JSON(http.StatusOK, response)
c.Render(http.StatusOK, openapi.MultipartRelatedRender{Data: response})
return
case nas.MsgTypePDUSessionModificationComplete:
smContext.StopT3591()
Expand Down Expand Up @@ -449,7 +449,7 @@ func (p *Processor) HandlePDUSessionSMContextUpdate(
logger.CtxLog.Infof("Skip sending PFCP Session Modification Request of PDUSessionID:%d of SUPI:%s",
smContext.PDUSessionID, smContext.Supi)
response.JsonData.UpCnxState = models.UpCnxState_DEACTIVATED
c.JSON(http.StatusOK, response)
c.Render(http.StatusOK, openapi.MultipartRelatedRender{Data: response})
return
}
smContext.SetState(smf_context.ModificationPending)
Expand Down Expand Up @@ -775,7 +775,7 @@ func (p *Processor) HandlePDUSessionSMContextUpdate(
case smf_context.SessionUpdateSuccess:
smContext.Log.Traceln("In case SessionUpdateSuccess")
smContext.SetState(smf_context.Active)
c.JSON(http.StatusOK, response)
c.Render(http.StatusOK, openapi.MultipartRelatedRender{Data: response})
case smf_context.SessionUpdateFailed:
smContext.Log.Traceln("In case SessionUpdateFailed")
smContext.SetState(smf_context.Active)
Expand All @@ -792,7 +792,7 @@ func (p *Processor) HandlePDUSessionSMContextUpdate(

smContext.Log.Traceln("In case SessionReleaseSuccess")
smContext.SetState(smf_context.InActivePending)
c.JSON(http.StatusOK, response)
c.Render(http.StatusOK, openapi.MultipartRelatedRender{Data: response})

case smf_context.SessionReleaseFailed:
// Update SmContext Request(N1 PDU Session Release Request)
Expand Down Expand Up @@ -822,12 +822,12 @@ func (p *Processor) HandlePDUSessionSMContextUpdate(
case smf_context.ModificationPending:
smContext.Log.Traceln("In case ModificationPending")
smContext.SetState(smf_context.Active)
c.JSON(http.StatusOK, response)
c.Render(http.StatusOK, openapi.MultipartRelatedRender{Data: response})
case smf_context.InActive, smf_context.InActivePending:
smContext.Log.Traceln("In case InActive, InActivePending")
c.JSON(http.StatusOK, response)
c.Render(http.StatusOK, openapi.MultipartRelatedRender{Data: response})
default:
c.JSON(http.StatusOK, response)
c.Render(http.StatusOK, openapi.MultipartRelatedRender{Data: response})
}

if smContext.PDUSessionRelease_DUE_TO_DUP_PDU_ID {
Expand Down Expand Up @@ -1046,7 +1046,7 @@ func (p *Processor) makeEstRejectResAndReleaseSMContext(
N1SmMsg: &models.RefToBinaryData{ContentId: "n1SmMsg"},
},
}
c.JSON(int(sbiError.Status), postSmContextsError)
p.nasErrorResponse(c, int(sbiError.Status), postSmContextsError)
} else {
postSmContextsError := models.PostSmContextsErrorResponse{
JsonData: &models.SmContextCreateError{
Expand All @@ -1055,7 +1055,7 @@ func (p *Processor) makeEstRejectResAndReleaseSMContext(
},
BinaryDataN1SmMessage: buf,
}
c.JSON(int(sbiError.Status), postSmContextsError)
p.nasErrorResponse(c, int(sbiError.Status), postSmContextsError)
}
p.RemoveSMContextFromAllNF(smContext, false)
}
Expand Down Expand Up @@ -1154,3 +1154,19 @@ func (p *Processor) sendGSMPDUSessionModificationCommand(smContext *smf_context.
})
}
}

func (p *Processor) nasErrorResponse(
c *gin.Context,
status int,
errBody models.PostSmContextsErrorResponse,
) {
switch status {
case http.StatusForbidden,
// http.StatusNotFound,
http.StatusInternalServerError,
http.StatusGatewayTimeout:
c.Render(status, openapi.MultipartRelatedRender{Data: errBody})
default:
c.JSON(status, errBody)
}
}
28 changes: 21 additions & 7 deletions internal/sbi/processor/pdu_session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,15 +457,17 @@ func TestHandlePDUSessionSMContextCreate(t *testing.T) {
request models.PostSmContextsRequest
paramStr string
resultStr string
responseBody any
expectedHTTPRsp *httpwrapper.Response
}{
{
initFuncs: []func(){initDiscUDMStubNRF, initDiscPCFStubNRF, initSMPoliciesPostStubPCF, initDiscAMFStubNRF},
request: models.PostSmContextsRequest{
BinaryDataN1SmMessage: buildPDUSessionModificationRequest(10, 1),
},
paramStr: "input wrong GSM Message type\n",
resultStr: "PDUSessionSMContextCreate should fail due to wrong GSM type\n",
paramStr: "input wrong GSM Message type\n",
resultStr: "PDUSessionSMContextCreate should fail due to wrong GSM type\n",
responseBody: &models.PostSmContextsErrorResponse{},
expectedHTTPRsp: &httpwrapper.Response{
Header: nil,
Status: http.StatusForbidden,
Expand Down Expand Up @@ -508,8 +510,9 @@ func TestHandlePDUSessionSMContextCreate(t *testing.T) {
},
BinaryDataN1SmMessage: buildPDUSessionEstablishmentRequest(10, 2, nasMessage.PDUSessionTypeIPv6),
},
paramStr: "try request the IPv6 PDU session\n",
resultStr: "Reject IPv6 PDU Session and respond error\n",
paramStr: "try request the IPv6 PDU session\n",
resultStr: "Reject IPv6 PDU Session and respond error\n",
responseBody: &models.PostSmContextsErrorResponse{},
expectedHTTPRsp: &httpwrapper.Response{
Header: nil,
Status: http.StatusForbidden,
Expand Down Expand Up @@ -560,8 +563,9 @@ func TestHandlePDUSessionSMContextCreate(t *testing.T) {
},
BinaryDataN1SmMessage: buildPDUSessionEstablishmentRequest(10, 3, nasMessage.PDUSessionTypeIPv4),
},
paramStr: "input correct PostSmContexts Request\n",
resultStr: "PDUSessionSMContextCreate should pass\n",
paramStr: "input correct PostSmContexts Request\n",
resultStr: "PDUSessionSMContextCreate should pass\n",
responseBody: &models.PostSmContextsResponse{},
expectedHTTPRsp: &httpwrapper.Response{
Header: nil,
Status: http.StatusCreated,
Expand Down Expand Up @@ -612,11 +616,21 @@ func TestHandlePDUSessionSMContextCreate(t *testing.T) {
t.Fatalf("Failed to close response body: %+v", errClose)
}

respBytes, errReadAll := io.ReadAll(httpResp.Body)
rawBytes, errReadAll := io.ReadAll(httpResp.Body)
if errReadAll != nil {
t.Fatalf("Failed to read response body: %+v", errReadAll)
}

err = openapi.Deserialize(tc.responseBody, rawBytes, httpResp.Header.Get("Content-Type"))
if err != nil {
t.Fatalf("Failed to deserialize response body: %+v", err)
}

respBytes, errMarshal := json.Marshal(tc.responseBody)
if errMarshal != nil {
t.Fatalf("Failed to marshal actual response body: %+v", errMarshal)
}

expectedBytes, errMarshal := json.Marshal(tc.expectedHTTPRsp.Body)
if errMarshal != nil {
t.Fatalf("Failed to marshal expected response body: %+v", errMarshal)
Expand Down

0 comments on commit ae96ce5

Please sign in to comment.