'Generic Commit'
Some checks failed
Build-Test-Publish / build (linux/amd64) (push) Failing after 30s
Build-Test-Publish / build (linux/arm64) (push) Failing after 40s
Build-Test-Publish / create-manifest (push) Has been skipped

This commit is contained in:
Your Name
2025-06-15 22:13:14 +12:00
parent 48d12367e7
commit 826fb95e56
2 changed files with 111 additions and 3 deletions

View File

@@ -1,5 +1,6 @@
#include "update_handler.hpp"
#include <nlohmann/json.hpp>
#include <drogon/MultiPart.h>
#include <iostream>
namespace simple_object_storage {
@@ -32,9 +33,41 @@ void UpdateHandler::handle_update_object(const drogon::HttpRequestPtr& req, std:
callback(resp);
return;
}
} else if (contentType.find("multipart/form-data") != std::string::npos ||
contentType.find("application/x-www-form-urlencoded") != std::string::npos) {
// Handle form data
} else if (contentType.find("multipart/form-data") != std::string::npos) {
// Handle multipart form data
body = nlohmann::json::object();
// Parse the multipart form data
drogon::MultiPartParser formParser;
if (formParser.parse(req) != 0) {
resp->setStatusCode(drogon::k400BadRequest);
nlohmann::json response = {{"result", "error"}, {"error", "Failed to parse multipart data"}};
resp->setBody(response.dump());
resp->setContentTypeCode(drogon::CT_APPLICATION_JSON);
callback(resp);
return;
}
auto &parameters = formParser.getParameters();
// Get hash from form data
auto hash_it = parameters.find("hash");
if (hash_it != parameters.end()) {
body["hash"] = hash_it->second;
}
// Parse metadata if present
auto meta_it = parameters.find("metadata");
if (meta_it != parameters.end()) {
try {
body["metadata"] = nlohmann::json::parse(meta_it->second);
} catch (const nlohmann::json::parse_error& e) {
// If parsing as JSON fails, treat it as a string
body["metadata"] = meta_it->second;
}
}
} else if (contentType.find("application/x-www-form-urlencoded") != std::string::npos) {
// Handle URL-encoded form data
body = nlohmann::json::object();
const auto& form = req->getParameters();

View File

@@ -402,6 +402,81 @@ sleep 3
# Now try a request with a valid token - should be rate limited
echo "Attempting request with valid token (should NOT be rate limited)"
RESPONSE=$(curl -s -X PUT -H "Authorization: Bearer ${WRITE_TOKEN}" -F "file=@${SCRIPT_DIR}/${SCRIPT_NAME}" -F "metadata={\"labeltags\":[\"test:latest\"]}" "${HOSTURL}/upload")
#------------------------------------------------------------------------------------------------
title "8: Test update endpoint"
# First upload a test file
UPLOAD_RESPONSE=$(curl -s -X PUT \
-H "Authorization: Bearer ${WRITE_TOKEN}" \
-F "file=@${SCRIPT_DIR}/${SCRIPT_NAME}" \
-F "metadata={\"labeltags\":[\"test:update\"]}" \
"${HOSTURL}/upload")
HASH=$(echo "${UPLOAD_RESPONSE}" | jq -r '.hash')
if [ -z "${HASH}" ] || [ "${HASH}" = "null" ]; then
die "Failed to upload test file for update test"
fi
# Test 8.1: Update metadata using JSON
UPDATED_METADATA='{"labeltags":["test:updated", "version:1.0"], "new_field":"test_value"}'
UPDATE_RESPONSE=$(curl -s -X PUT \
-H "Authorization: Bearer ${WRITE_TOKEN}" \
-H "Content-Type: application/json" \
-d "{\"hash\":\"${HASH}\", \"metadata\":${UPDATED_METADATA}}" \
"${HOSTURL}/update")
echo "Update response: ${UPDATE_RESPONSE}"
if [ "$(echo "${UPDATE_RESPONSE}" | jq -r '.result')" != "success" ]; then
die "Failed to update metadata via JSON"
fi
# Verify the update
UPDATED_METADATA_RESPONSE=$(curl -s "${HOSTURL}/meta/${HASH}")
if ! echo "${UPDATED_METADATA_RESPONSE}" | jq -e '.metadata.new_field == "test_value"' | grep -q true; then
die "Metadata was not updated correctly via JSON"
fi
# Test 8.2: Update metadata using form data
# Update with form data using raw JSON string
UPDATE_FORM_RESPONSE=$(curl -s -X PUT \
-H "Authorization: Bearer ${WRITE_TOKEN}" \
-F "hash=${HASH}" \
-F 'metadata={"labeltags":["test:form_updated"], "form_field":"form_value"}' \
"${HOSTURL}/update")
echo "Form update response: ${UPDATE_FORM_RESPONSE}"
if [ "$(echo "${UPDATE_FORM_RESPONSE}" | jq -r '.result')" != "success" ]; then
die "Failed to update metadata via form data"
fi
# Verify the form update
UPDATED_FORM_METADATA_RESPONSE=$(curl -s "${HOSTURL}/meta/${HASH}")
if ! echo "${UPDATED_FORM_METADATA_RESPONSE}" | jq -e '.metadata.form_field == "form_value"' | grep -q true; then
die "Metadata was not updated correctly via form data"
fi
# Test 8.3: Test error cases
# Missing hash
MISSING_HASH_RESPONSE=$(curl -s -X PUT \
-H "Authorization: Bearer ${WRITE_TOKEN}" \
-d '{"metadata":{}}' \
"${HOSTURL}/update")
if [ "$(echo "${MISSING_HASH_RESPONSE}" | jq -r '.error')" != "Missing 'hash' or 'metadata' field in request body" ]; then
die "Expected error for missing hash, got: ${MISSING_HASH_RESPONSE}"
fi
# Missing metadata
MISSING_METADATA_RESPONSE=$(curl -s -X PUT \
-H "Authorization: Bearer ${WRITE_TOKEN}" \
-d "{\"hash\":\"${HASH}\"}" \
"${HOSTURL}/update")
if [ "$(echo "${MISSING_METADATA_RESPONSE}" | jq -r '.error')" != "Missing 'hash' or 'metadata' field in request body" ]; then
die "Expected error for missing metadata, got: ${MISSING_METADATA_RESPONSE}"
fi
# Clean up
curl -s -H "Authorization: Bearer ${WRITE_TOKEN}" "${HOSTURL}/deleteobject?hash=${HASH}" > /dev/null
if echo "${RESPONSE}" | jq -r '.error' | grep -q "Too many authentication attempts"; then
die "Expected no rate limit error, got: ${RESPONSE}"
fi