1 | #line 2 "testBegin.ino"
|
---|
2 | /*
|
---|
3 | testWriteField unit test
|
---|
4 |
|
---|
5 | Unit Test for the writeField function in the ThingSpeak Communication Library for Arduino
|
---|
6 |
|
---|
7 | ThingSpeak ( https://www.thingspeak.com ) is a free IoT service for prototyping
|
---|
8 | systems that collect, analyze, and react to their environments.
|
---|
9 |
|
---|
10 | This test use the ArduinoUnit 2.1.0 unit test framework. Visit https://github.com/mmurdoch/arduinounit to learn more.
|
---|
11 |
|
---|
12 | Copyright 2015, The MathWorks, Inc.
|
---|
13 |
|
---|
14 | Documentation for the ThingSpeak Communication Library for Arduino is in the extras/documentation folder where the library was installed.
|
---|
15 | See the accompaning licence.txt file for licensing information.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include <ArduinoUnit.h>
|
---|
19 |
|
---|
20 | #ifdef ARDUINO_AVR_YUN
|
---|
21 | #include <SPI.h>
|
---|
22 | #include "YunClient.h"
|
---|
23 | YunClient client;
|
---|
24 | #else
|
---|
25 | // Assume that we're using a wired Ethernet shield on a Mega
|
---|
26 | #include <SPI.h>
|
---|
27 | #include <Ethernet.h>
|
---|
28 | byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
|
---|
29 | EthernetClient client;
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include <ThingSpeak.h>
|
---|
33 |
|
---|
34 | unsigned long testChannelNumber = 31461;
|
---|
35 | const char * testChannelWriteAPIKey = "LD79EOAAWRVYF04Y";
|
---|
36 |
|
---|
37 | test(beginCase)
|
---|
38 | {
|
---|
39 | assertTrue(ThingSpeak.begin(client));
|
---|
40 |
|
---|
41 | assertTrue(ThingSpeak.begin(client,IPAddress(1,2,3,4),80));
|
---|
42 |
|
---|
43 | assertTrue(ThingSpeak.begin(client,"www.mathworks.com",80));
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | test(badAddresses)
|
---|
48 | {
|
---|
49 | // Test for valid, but incorrect, URL (en.wikipedia.org) that gives a 404 response
|
---|
50 | assertTrue(ThingSpeak.begin(client,"en.wikipedia.org",80));
|
---|
51 | assertEqual(ERR_BADURL, ThingSpeak.writeField(testChannelNumber, 1, (float)1.0, testChannelWriteAPIKey));
|
---|
52 |
|
---|
53 | // Test for non-existant URL (http://www.iwishthiswebsitewerereal.com/)
|
---|
54 | assertTrue(ThingSpeak.begin(client,"www.iwishthiswebsitewerereal.com",80));
|
---|
55 | #ifdef ARDUINO_AVR_YUN
|
---|
56 | // Yun gives -301 response
|
---|
57 | int badURLResponse = ERR_CONNECT_FAILED;
|
---|
58 | #else
|
---|
59 | // Ethernet shield gives -302 response
|
---|
60 | int badURLResponse = ERR_UNEXPECTED_FAIL;
|
---|
61 | #endif
|
---|
62 | assertEqual(badURLResponse, ThingSpeak.writeField(testChannelNumber, 1, (float)2.0, testChannelWriteAPIKey));
|
---|
63 |
|
---|
64 | // Test for non-existant IP 192.168.1.234
|
---|
65 | assertTrue(ThingSpeak.begin(client,IPAddress(192,168,1,234),80));
|
---|
66 | assertEqual(-301, ThingSpeak.writeField(testChannelNumber, 1, (float)2.0, testChannelWriteAPIKey));
|
---|
67 |
|
---|
68 | //Test for bad suburl (badapi.thingspeak.com)
|
---|
69 | assertTrue(ThingSpeak.begin(client,"invalid.thingspeak.com",80));
|
---|
70 | assertEqual(badURLResponse, ThingSpeak.writeField(testChannelNumber, 1, (float)4.0, testChannelWriteAPIKey));
|
---|
71 | }
|
---|
72 |
|
---|
73 | void setup()
|
---|
74 | {
|
---|
75 | Serial.begin(9600);
|
---|
76 | while(!Serial); // for the Arduino Leonardo/Micro only
|
---|
77 | #ifdef ARDUINO_AVR_YUN
|
---|
78 | Bridge.begin();
|
---|
79 | #else
|
---|
80 | Ethernet.begin(mac);
|
---|
81 | #endif
|
---|
82 | }
|
---|
83 |
|
---|
84 | void loop()
|
---|
85 | {
|
---|
86 | Test::run();
|
---|
87 | }
|
---|
88 |
|
---|