Skip to content

Commit

Permalink
[web] fix rest-test issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tttttangTH committed Oct 19, 2020
1 parent 36d370a commit 1e2ef96
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 34 deletions.
28 changes: 7 additions & 21 deletions src/rest/resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,6 @@ Resource::Resource(ControllerOpenThread *aNcp)
mResourceCallbackMap.emplace(OT_REST_RESOURCE_PATH_DIAGNOETIC, &Resource::HandleDiagnosticCallback);
}

void Resource::Init(void)
{
otThreadSetReceiveDiagnosticGetCallback(mInstance, &Resource::DiagnosticResponseHandler, this);
}

void Resource::Handle(Request &aRequest, Response &aResponse) const
{
std::string url = aRequest.GetUrl();
Expand Down Expand Up @@ -518,31 +513,22 @@ void Resource::UpdateDiag(std::string aKey, std::vector<otNetworkDiagTlv> &aDiag

void Resource::Diagnostic(const Request &aRequest, Response &aResponse) const
{
otbrError error = OTBR_ERROR_NONE;
otThreadSetReceiveDiagnosticGetCallback(mInstance, &Resource::DiagnosticResponseHandler,
const_cast<Resource *>(this));
OT_UNUSED_VARIABLE(aRequest);
struct otIp6Address rloc16address = *otThreadGetRloc(mInstance);
struct otIp6Address multicastAddress;

VerifyOrExit(otThreadSendDiagnosticGet(mInstance, &rloc16address, kAllTlvTypes, sizeof(kAllTlvTypes)) ==
OT_ERROR_NONE,
error = OTBR_ERROR_REST);
VerifyOrExit(otIp6AddressFromString(kMulticastAddrAllRouters, &multicastAddress) == OT_ERROR_NONE,
error = OTBR_ERROR_REST);
OT_ERROR_NONE);
VerifyOrExit(otIp6AddressFromString(kMulticastAddrAllRouters, &multicastAddress) == OT_ERROR_NONE);
VerifyOrExit(otThreadSendDiagnosticGet(mInstance, &multicastAddress, kAllTlvTypes, sizeof(kAllTlvTypes)) ==
OT_ERROR_NONE,
error = OTBR_ERROR_REST);
OT_ERROR_NONE);

exit:

if (error == OTBR_ERROR_NONE)
{
aResponse.SetStartTime(steady_clock::now());
aResponse.SetCallback();
}
else
{
ErrorHandler(aResponse, HttpStatusCode::kStatusInternalServerError);
}
aResponse.SetStartTime(steady_clock::now());
aResponse.SetCallback();
}

void Resource::DiagnosticResponseHandler(otMessage *aMessage, const otMessageInfo *aMessageInfo, void *aContext)
Expand Down
7 changes: 0 additions & 7 deletions src/rest/resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ class Resource
*/
Resource(ControllerOpenThread *aNcp);

/**
* This method initialize the Resource handler.
*
*
*/
void Init(void);

/**
* This method is the main entry of resource handler, which find corresponding handler according to request url
* find the resource and set the content of response.
Expand Down
2 changes: 0 additions & 2 deletions src/rest/rest_web_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ otbrError RestWebServer::Init(void)
{
otbrError error = OTBR_ERROR_NONE;

mResource.Init();

error = InitializeListenFd();

return error;
Expand Down
4 changes: 4 additions & 0 deletions tests/rest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ set_tests_properties(rest-server PROPERTIES
set_tests_properties(rest-server PROPERTIES
LABELS "TESTREST"
)

set_tests_properties(rest-server PROPERTIES
TIMEOUT 60
)
6 changes: 6 additions & 0 deletions tests/rest/test-rest-server
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ main()
sudo "${CMAKE_BINARY_DIR}"/src/agent/otbr-agent -d 7 -v -I wpan0 "spinel+hdlc+forkpty://$(command -v ot-rcp)?forkpty-arg=1" &
trap on_exit EXIT
sleep 5
sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl factoryreset
sleep 1
sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl ifconfig up
sleep 1
sudo "${CMAKE_BINARY_DIR}"/third_party/openthread/repo/src/posix/ot-ctl thread start
sleep 3
sudo python3 "${CMAKE_CURRENT_SOURCE_DIR}"/test_rest.py
}

Expand Down
14 changes: 10 additions & 4 deletions tests/rest/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@


def get_data_from_url(url, result, index):
response = urllib.request.urlopen(urllib.request.Request(url))
body = response.read()
data = json.loads(body)
result[index] = data

try:
response = urllib.request.urlopen(urllib.request.Request(url))
body = response.read()
data = json.loads(body)
result[index] = data


except urllib.error.HTTPError as e:
# Todo : print detail debug message from response
print(e.code)

def get_error_from_url(url, result, index):
try:
Expand Down

0 comments on commit 1e2ef96

Please sign in to comment.