Skip to content

Commit

Permalink
Merge pull request #118 from ubirch/CPI-398-implement-niomon-resp-header
Browse files Browse the repository at this point in the history
CPI-398 Add X-Err codes into response header
  • Loading branch information
leroxyl authored Feb 22, 2023
2 parents a20ec81 + 8af303e commit e729d43
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
5 changes: 5 additions & 0 deletions main/adapters/handlers/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ var (
Header: http.Header{"test": []string{"header"}},
Content: testBckndRespUPP,
}
testBckndConflictResp = h.HTTPResponse{
StatusCode: http.StatusConflict,
Header: http.Header{"X-Err": []string{"NF409-0000"}},
Content: testBckndRespUPP,
}
testVerificationResp = []byte(fmt.Sprintf("{\"upp\":\"%s\",\"prev\":null,\"anchors\":null}", base64.StdEncoding.EncodeToString(testSignedUPP)))
testKeyRegs = []ubirch.SignedKeyRegistration{{PubKeyInfo: ubirch.KeyRegistration{PubKey: base64.StdEncoding.EncodeToString(testPublicKey)}}}
)
10 changes: 9 additions & 1 deletion main/adapters/handlers/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,17 @@ func getHTTPResponse(statusCode int, signingResp *signingResponse) h.HTTPRespons
log.Warnf("error serializing signing response: %v", err)
}

headers := http.Header{"Content-Type": {h.JSONType}}
if signingResp.Response != nil && statusCode != http.StatusOK {
errValues := signingResp.Response.Header.Values(h.XErrorHeader)
for _, value := range errValues {
headers.Add(h.XErrorHeader, value)
}
}

return h.HTTPResponse{
StatusCode: statusCode,
Header: http.Header{"Content-Type": {"application/json"}},
Header: headers,
Content: respContent,
}
}
37 changes: 37 additions & 0 deletions main/adapters/handlers/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,43 @@ func TestSigner_Sign(t *testing.T) {
}), resp)
},
},
{
name: "anchor online conflict",
msg: h.HTTPRequest{
Ctx: context.Background(),
ID: testUuid,
Auth: testAuth,
Hash: testHash,
Operation: h.AnchorHash,
Offline: false,
},
setMockBehavior: func(m *mock.Mock) {
m.On("LoadActiveFlag", testUuid).Return(true, nil)
m.On("Sign", &ubirch.SignedUPP{
Version: ubirch.Signed,
Uuid: testUuid,
Hint: ubirch.Binary,
Payload: testHash[:],
}).Return(testSignedUPP, nil)
m.On("GetPublicKeyBytes", testUuid).Return(testPublicKey, nil)
m.On("sendToAuthService", testUuid, testAuth, testSignedUPP).Return(testBckndConflictResp, nil)
m.On("VerifyBackendResponse", testSignedUPP, testBckndConflictResp.Content).Return(true, true, nil)
},
tcChecks: func(t *testing.T, resp h.HTTPResponse, m *mock.Mock) {
m.AssertExpectations(t)
assert.Equal(t, resp.Header.Get("X-Err"), "NF409-0000")
assert.Equal(t, getHTTPResponse(http.StatusConflict, &signingResponse{
Hash: testHash[:],
Operation: string(h.AnchorHash),
UPP: testSignedUPP,
PublicKey: testPublicKey,
Response: &testBckndConflictResp,
RequestID: testRequestID,
ResponseSignatureVerified: true,
ResponseChainVerified: true,
}), resp)
},
},
{
name: "anchor offline",
msg: h.HTTPRequest{
Expand Down
5 changes: 3 additions & 2 deletions main/adapters/http_server/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ const (
TextType = "text/plain"
JSONType = "application/json"

XUPPHeader = "X-Ubirch-UPP"
XAuthHeader = "X-Auth-Token"
XUPPHeader = "X-Ubirch-UPP"
XAuthHeader = "X-Auth-Token"
XErrorHeader = "X-Err"

HexEncoding = "hex"

Expand Down

0 comments on commit e729d43

Please sign in to comment.