New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor http API handler, add test #199
refactor http API handler, add test #199
Conversation
vixentael
commented
Jul 12, 2018
- add tests for HTTP response codes
- refactor HTTP handler into smaller functions
cmd/acra-reader/server.go
Outdated
if zoneId != nil { | ||
privateKey, err = server.keystorage.GetZonePrivateKey(zoneId) | ||
} else { | ||
privateKey, err = server.keystorage.GetZonePrivateKey(clientId) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetZonePrivateKey
-> GetServerDecryptionPrivateKey
cmd/acra-reader/server.go
Outdated
} | ||
|
||
|
||
func (server *ReaderServer) closeConnectionAndSendResponse(logger *log.Entry, response *http.Response, connection net.Conn) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename to sendResponseAndCloseConnection
?)
cmd/acra-reader/server.go
Outdated
logger.WithError(err).WithField(logging.FieldKeyEventCode, logging.EventCodeErrorReaderCantReturnResponse). | ||
Warningln("Can't write response to HTTP request") | ||
} else { | ||
connection.Write(buf.Bytes()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log errors on write
cmd/acra-reader/server.go
Outdated
connection.Write(buf.Bytes()) | ||
} | ||
|
||
connection.Close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
log errors on close
cmd/acra-reader/acra-reader_test.go
Outdated
request.URL, _ = url.Parse("http://smth.com/v1/decrypt?zone_id=\"somezoneid\"") | ||
request.Body = ioutil.NopCloser(bytes.NewBufferString("bla bla bla body")) | ||
res = readerServer.parseRequestPrepareResponse(logger, &request, []byte("asdf")) | ||
if res.StatusCode != http.StatusUnprocessableEntity { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check decryption success request too?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it depends on keystorage, masterkey and folders. is there any way to mock keystorage behaviour (to override getting private keys functiont)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it ok to use https://github.com/golang/mock?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's just interface and you can add simple as you need implementation for tests (example https://github.com/cossacklabs/acra/pull/200/files#diff-2a5db1c46f9ea9f86e34328a7bbc0dc6R13)
cmd/acra-reader/server.go
Outdated
} | ||
|
||
// /v1/decrypt | ||
// /, v1, decrypt | ||
pathParts := strings.Split(request.URL.Path, string(os.PathSeparator)) | ||
if len(pathParts) != 3 { | ||
logger.WithError(err).WithField(logging.FieldKeyEventCode, logging.EventCodeErrorReaderMalformedURL). | ||
logger.WithField(logging.FieldKeyEventCode, logging.EventCodeErrorReaderMalformedURL). | ||
Warningf("Malformed URL, expected /<version>/<endpoint>, got %s", request.URL.Path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we place this explanation to response body to simplify understanding of errors by clients?
* acra reader: entry point * deadline listener; refactor acra-server * base part of connection handling (#197) * base part of connection handling * save connections by descriptor * add a little bit graceful stop service * basic http /decrypt handle (#198) * refactor http API handler, add test (#199) * refactor http API handler, add test * validate acrastruct length before decryption (#201) * add more errors checking * copy keys in test keystorage, add tests on encryption * move into separate folder; add http message responses in body * grpc handler (#200) * change sign of comparison key length * base part of connection handling * save connections by descriptor * add a little bit graceful stop service * add grpc service * fix log message * fix import path * add missing file * validate acrastruct length before decryption * add api test * drop old method fill with zeroes private key * verbose go get * rename api -> grpc_api * fix network part (#203) * fix accepting connections, network manager * drop comment * AcraReader http buffering (#204) * fixing http handling * close connection, remove buffers for http response * add more logs to decryptor * move defer to the top * acra-reader -> acra-translator renaming (#205) * acra-reader -> acra-translator * re-gen api.pb.go * rm old file * Update AcraConnector to connect with AcraTranslator (#206) * acra-reader -> acra-translator * re-gen api.pb.go * add separate acra-translator keys * simplify keys check * acra reader integration tests [T663] (#207) * integration tests for grpc/http * refactor serializing http response struct * check response codes and messages in tests * compare all messages in lower case * add translator keystore move filesystem keystore to separate package run translator with own transport keys * fix import in unit test * return error code after failed creation of posion record * drop keys folder before tests * set Connection: close header to http response * optimize response sending * deadline for secure session handshake timeout for http requests in tests * refactor tests of acra-connector shutdown * detect poison record on translator (#212) * check poison records on acra-translator * change path to test master key * update log messages