Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit 7a5c827

Browse files
committed
Untested initial command with tests
Add tests Added gmock/gtest submodules Revert "Added gmock/gtest submodules" This reverts commit b975f3b. found out googlemock and googletest are now merged, only need one. Added googletest submodule Added *Ptr methods to firebase, get test mostly compiling Broke FirebaseHttpClient out from Firebase to allow greater portability and testability. re check in commands and add input stream Working local command test Added base test for set Added base test for set (actually this time) Cleanup set test and add error test Clean up get test and added failure handling Added remove command and test Added push command and test Minor cleanup to set test Added begin command and test Fixed all tests operating on the assumption that readLine() included the \r\n
1 parent 2eaf813 commit 7a5c827

21 files changed

+842
-79
lines changed

FirebaseHttpClient.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
#define FIREBASE_HTTP_CLIENT_H
33

44
#include "Arduino.h"
5+
#include "Stream.h"
6+
7+
struct HttpStatus {
8+
static const int TEMPORARY_REDIRECT = 302;
9+
};
510

611
class FirebaseHttpClient {
712
public:
@@ -14,7 +19,7 @@ class FirebaseHttpClient {
1419
virtual void end() = 0;
1520

1621
virtual void addHeader(const String& name, const String& value) = 0;
17-
virtual void collectHeaders(const String[]& header_keys,
22+
virtual void collectHeaders(const String header_keys[],
1823
const int header_key_count) = 0;
1924
virtual String header(const String& name) = 0;
2025

@@ -27,9 +32,10 @@ class FirebaseHttpClient {
2732
virtual String errorToString(int error_code) = 0;
2833

2934
protected:
30-
static final String kFirebaseFingerprint = "7A 54 06 9B DC 7A 25 B3 86 8D 66 53 48 2C 0B 96 42 C7 B3 0A";
31-
static final const uint16_t kFirebasePort = 443;
32-
}
35+
static const uint16_t kFirebasePort = 443;
36+
};
3337

38+
static const String kFirebaseFingerprint =
39+
"7A 54 06 9B DC 7A 25 B3 86 8D 66 53 48 2C 0B 96 42 C7 B3 0A";
3440

3541
#endif // FIREBASE_HTTP_CLIENT_H

FirebaseHttpClient_Esp8266.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class FirebaseHttpClientEsp8266 : public FirebaseHttpClient {
6666

6767
private:
6868
HTTPClient http_;
69-
}
69+
};
7070

7171
Firebase* FirebaseHttpClient::create() {
7272
return new FirebaseHttpClientEsp8266();

FirebaseHttpClient_dummy.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,36 @@
1+
#ifdef __GNUC__
2+
# define UNUSED_ARG(x) UNUSED_ ## x __attribute__((__unused__))
3+
#else
4+
# define UNUSED_ARG(x) UNUSED_ ## x
5+
#endif
16

27
#include "FirebaseHttpClient.h"
38

49
class FirebaseHttpClientDummy : public FirebaseHttpClient {
510
public:
6-
void SetReuseConnection(bool reuse) override {
11+
void setReuseConnection(bool UNUSED_ARG(reuse)) override {
712
}
813

9-
void begin(const String& url) override {
14+
void begin(const String& UNUSED_ARG(url)) override {
1015
}
1116

12-
void begin(const String& host, const String& path) override {
17+
void begin(const String& UNUSED_ARG(host), const String& UNUSED_ARG(path)) override {
1318
}
1419

1520
void end() override {
1621
}
1722

18-
void addHeader(const String& name, const String& value) override {
23+
void addHeader(const String& UNUSED_ARG(name), const String& UNUSED_ARG(value)) override {
1924
}
2025

21-
void collectHeaders(const String[]& header_keys, const int count) override {
26+
void collectHeaders(const String UNUSED_ARG(header_keys[]), const int UNUSED_ARG(count)) override {
2227
}
2328

24-
String header(const String& name) override {
29+
String header(const String& UNUSED_ARG(name)) override {
2530
return "";
2631
}
2732

28-
int sendRequest(const String& method, const String& data) override {
33+
int sendRequest(const String& UNUSED_ARG(method), const String& UNUSED_ARG(data)) override {
2934
return 0;
3035
}
3136

@@ -34,15 +39,15 @@ class FirebaseHttpClientDummy : public FirebaseHttpClient {
3439
}
3540

3641
Stream* getStreamPtr() override {
37-
return http_.getStreamPtr();
42+
return nullptr;
3843
}
3944

40-
String errorToStream(int error_code) override {
41-
return String(status);
45+
String errorToString(int UNUSED_ARG(error_code)) override {
46+
return String();
4247
}
43-
}
48+
};
4449

45-
Firebase* FirebaseHttpClient::create() {
50+
FirebaseHttpClient* FirebaseHttpClient::create() {
4651
return new FirebaseHttpClientDummy();
4752
}
4853

modem/begin-command.cc

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "modem/commands.h"
2+
3+
namespace firebase {
4+
namespace modem {
5+
6+
bool BeginCommand::execute(const String& command,
7+
InputStream* in, OutputStream* out) {
8+
if (in == nullptr || out == nullptr) {
9+
return false;
10+
}
11+
12+
if (command != "BEGIN") {
13+
return false;
14+
}
15+
16+
String host;
17+
String auth;
18+
19+
String data(in->readLine());
20+
// Remove leading ' '.
21+
data = data.substring(1);
22+
23+
int space_index = data.indexOf(' ');
24+
if (space_index == -1) {
25+
// host only.
26+
host = data;
27+
} else {
28+
// host and auth.
29+
host = data.substr(0, space_index);
30+
auth = data.substr(space_index + 1);
31+
}
32+
33+
if (host.empty()) {
34+
out->println("-FAIL Missing host");
35+
return false;
36+
}
37+
38+
new_firebase_.reset(new Firebase(host));
39+
if (!auth.empty()) {
40+
new_firebase_->auth(auth);
41+
}
42+
43+
out->println("+OK");
44+
return true;
45+
}
46+
47+
std::unique_ptr<Firebase> BeginCommand::firebase() {
48+
return std::move(new_firebase_);
49+
}
50+
51+
} // modem
52+
} // firebase

modem/commands.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#ifndef MODEM_COMMAND_H
2+
#define MODEM_COMMAND_H
3+
4+
#include "Firebase.h"
5+
#include "modem/output-stream.h"
6+
#include "modem/input-stream.h"
7+
8+
namespace firebase {
9+
namespace modem {
10+
11+
class Command {
12+
public:
13+
Command(Firebase* fbase) : fbase_(fbase) {}
14+
15+
// Execute command, reading any additional data needed from stream.
16+
// Return false if execution failed.
17+
virtual bool execute(const String& command,
18+
InputStream* in, OutputStream* out) = 0;
19+
protected:
20+
Firebase& fbase() {
21+
return *fbase_;
22+
}
23+
24+
private:
25+
Firebase* fbase_;
26+
};
27+
28+
class GetCommand : public Command {
29+
public:
30+
GetCommand(Firebase* fbase) : Command(fbase) {}
31+
32+
bool execute(const String& command, InputStream* in, OutputStream* out);
33+
};
34+
35+
class SetCommand : public Command {
36+
public:
37+
SetCommand(Firebase* fbase) : Command(fbase) {}
38+
39+
bool execute(const String& command, InputStream* in, OutputStream* out);
40+
};
41+
42+
class RemoveCommand : public Command {
43+
public:
44+
RemoveCommand(Firebase* fbase) : Command(fbase) {}
45+
46+
bool execute(const String& command, InputStream* in, OutputStream* out);
47+
};
48+
49+
class PushCommand : public Command {
50+
public:
51+
PushCommand(Firebase* fbase) : Command(fbase) {}
52+
53+
bool execute(const String& command, InputStream* in, OutputStream* out);
54+
};
55+
56+
class BeginCommand : public Command {
57+
public:
58+
BeginCommand() : Command(nullptr) {}
59+
60+
bool execute(const String& command, InputStream* in, OutputStream* out);
61+
62+
// This can only be called once.
63+
std::unique_ptr<Firebase> firebase();
64+
65+
private:
66+
std::unique_ptr<Firebase> new_firebase_;
67+
};
68+
69+
} // modem
70+
} // firebase
71+
72+
#endif //MODEM_COMMAND_H

modem/get-command.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,18 @@ bool GetCommand::execute(const String& command,
1414
}
1515

1616
String path = in->readLine();
17-
std::unique_ptr<FirebaseGet> get = fbase().getPtr(path);
17+
std::unique_ptr<FirebaseGet> get(fbase().getPtr(path));
1818

19-
String value = get->json();
19+
if (get->error()) {
20+
out->print("-FAIL ");
21+
out->println(get->error().message());
22+
return false;
23+
}
24+
25+
String value(get->json());
2026
// TODO implement json parsing to pull and process value.
2127
out->print("+");
2228
out->println(value);
23-
2429
return true;
2530
}
2631

modem/input-stream.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef MODEM_INPUT_STREAM_H
2+
#define MODEM_INPUT_STREAM_H
3+
4+
namespace firebase {
5+
namespace modem {
6+
7+
class InputStream {
8+
public:
9+
virtual String readLine() = 0;
10+
virtual String readStringUntil(const char terminator) = 0;
11+
};
12+
13+
class SerialInputStream : public InputStream {
14+
public:
15+
String readLine() override {
16+
String out = Serial.readStringUntil('\r');
17+
if (Serial.peek() == '\n') {
18+
// This should be a '\n' so drop it.
19+
Serial.read();
20+
} else {
21+
// Add \r back, this is not our endline.
22+
out += '\r';
23+
24+
// Recurse until \r\n
25+
out += readLine();
26+
}
27+
return out;
28+
}
29+
String readStringUntil(const char terminator) {
30+
return Serial.readStringUntil(terminator);
31+
}
32+
};
33+
34+
} // modem
35+
} // firebase
36+
37+
#endif // MODEM_INPUT_STREAM_H
38+

modem/push-command.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "modem/commands.h"
2+
3+
namespace firebase {
4+
namespace modem {
5+
6+
bool PushCommand::execute(const String& command,
7+
InputStream* in, OutputStream* out) {
8+
if (in == nullptr || out == nullptr) {
9+
return false;
10+
}
11+
12+
if (command != "SET") {
13+
return false;
14+
}
15+
16+
String path(in->readStringUntil(' '));
17+
String data(in->readLine());
18+
19+
// First char will be a ' ', drop it.
20+
data = data.substring(1);
21+
22+
// TODO(ed7coyne): encode data as json.
23+
24+
std::unique_ptr<FirebasePush> push(fbase().pushPtr(path, data));
25+
26+
if (push->error()) {
27+
out->print("-FAIL ");
28+
out->println(push->error().message());
29+
return false;
30+
} else {
31+
out->println("+OK");
32+
return true;
33+
}
34+
}
35+
36+
} // modem
37+
} // firebase

modem/remove-command.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "modem/commands.h"
2+
3+
namespace firebase {
4+
namespace modem {
5+
6+
bool RemoveCommand::execute(const String& command,
7+
InputStream* in, OutputStream* out) {
8+
if (in == nullptr || out == nullptr) {
9+
return false;
10+
}
11+
12+
if (command != "REMOVE") {
13+
return false;
14+
}
15+
16+
String path = in->readLine();
17+
std::unique_ptr<FirebaseRemove> get(fbase().removePtr(path));
18+
19+
if (get->error()) {
20+
out->print("-FAIL ");
21+
out->println(get->error().message());
22+
return false;
23+
}
24+
25+
out->print("+OK");
26+
return true;
27+
}
28+
29+
} // modem
30+
} // firebase

0 commit comments

Comments
 (0)